如何使用Linq从C#中的void语句返回数据
我是LINQ的新手,并且没有太多使用此方法的经验。
我遇到的问题是我有一个void函数,想要使用LINQ返回一些数据。
代码:
Hi,
I am new to LINQ and do not have much experience using this method.
The problem I have come across is where I have a void function and want to return some data back using LINQ.
the code:
private void Test(string a, string b, string c, bool d, bool o, bool f, DateTime g, DateTime h, string i, string j, string k)
{
if (b != "")
var query = from a in PromoSuffixDispaly
where a.MasterSuffixCode == a.MasterSuffixCode && a.PromoSuffixCode == a.PromoSuffixCode && a.Pds_ItemType == "N" && a.Code.Substring(a.Code.Length - 1, 7) != a.orderAs_Product && a.Pds_DeletedIndicator == false
&& !(from a0 in PromoSuffixDispaly
where a0.MasterSuffixCode == a0.MasterSuffixCode && a0.PromoSuffixCode == a0.PromoSuffixCode
select new { a0.Pds_NatItemNumber }).ToString().Contains(a.Pds_NatItemNumber = a.BusinessUnit + " " + a.orderAs_Product)
select new { a };
}
此查询运行但所有数据都被恢复。并且RETURN语句将无效。我可以使用的任何建议或方式吗?
谢谢
从解决方案中添加:
当我放置断点并将鼠标悬停在查询后它没有显示任何内容,但在屏幕上显示所有数据被带回。
- OriginalGriff
[/ edit]
this query runs but all the data is brought back. and the RETURN statement will not work in a void. any suggestion or way that I can use?
thanks
[edit]
Added from "solution":
when I put my break point and hover over the query after it shows nothing, but on the screen all the data is brought back.
- OriginalGriff
[/edit]
你不能在void
方法中使用return
返回任何值 - 因为声明方法为void
明确地说这个方法没有返回值 - 所以外部世界在调用方法时不能期待任何回复。
如果如果要返回值,则需要更改方法的签名:要么包含返回类型,要么添加out
或ref
该方法可以设置为包含返回值的参数。
对于像你这样的Linq查询,你可能想回来IEnumerable< T>或者列表< T>
我无法确切地建议你想要返回什么类型 - 我不知道PromoSuffixDispaly包含什么类型 - 但你可以返回该类型的IEnumerable,或者使用在该IEnumerable上的ToList方法将其转换为List< T>然后返回。
You cannot usereturn
in avoid
method to return any value - becasue declaring a method asvoid
is explicitly saying "this method does no return a value" - so the outside world cannot expect anything back when it calls the method.
If you want to return a value, you need to change the "signature" of your method: either to include a return type, or to add anout
orref
parameter which the method can set to contain the return values.
In the case of a Linq Query such as yours, you probably want to return an IEnumerable<T> or a List<T>
I can't suggest exactly what type you want to return - I don't know what type PromoSuffixDispaly contains - but you could return the IEnumerable of that type, or use the ToList method on that IEnumerable to convert it to a List<T> and return that.