LINQ:有关 linq 查询的帮助并包含 IEnumerable?
有人可以帮忙吗?
我有一个嵌入在扩展方法中的 linq 查询,它的工作原理是 v.RentalStatus 是一个字符串。我现在在原始查询中使用一个组(查询非常复杂,所以我不会把它放在这里)。
重要的是 v.RentalStatus = IEnumerable 因此它可以包含诸如
A (meaning active)
R (meaning rented)
U (unavailable)
etc - many more
我创建一个我想要返回的列表并将其存储在 statusStringList 中的内容,因此例如让我们假设该列表包含 A 和 R
这是我的代码之前,当 v.RentalStatus 只是一个字符串时,任何人都可以告诉我如何修改它才能工作。
var statusStringList = rentalStatus.ToList().ConvertAll<string>(st => st.GetStringValue());
return from v in qry
where statusStringList.Contains(v.RentalStatus)
select v;
如果有帮助的话,这是我的查询的一部分,它返回 RentalStatus - 它是组查询的一部分,但 RentalStatus 不在组中
RentalStatus= g1.Select( j => j.IdRentalStatus).Distinct(),
g1 是我的组,所以如果您想象有 10 个“A”,5 个“U” ..然后它将返回 A 和 U 的可枚举...因为我正在使用 Distinct。不是 10 个 As 和 5 个我们,
我希望我已经解释得很好,如果没有,请告诉我,
我将不胜感激任何人的帮助..
谢谢
编辑
这是我的扩展签名,但并不重要。
public static IQueryable<Rentals> WithStatus(this IQueryable<Rentals> qry, IList<Contants.Statuses> rentalStatus)
{
编辑
如前所述,当 v.RentalStatus 是一个字符串时,它正在工作,但现在它是 IEnumerable - 因此是一个集合..并且出现此错误
Argument '1': cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'string'
Can anyone help?
I have a linq query which is embedded inside a extension method, it was working as v.RentalStatus was a String. I am now using a Group on my original query (the query is quite complex so i won't put it here).
The importante thing is that v.RentalStatus = IEnumerable hence it can contain things like
A (meaning active)
R (meaning rented)
U (unavailable)
etc - many more
I create a list of what i would like to get back and store this in statusStringList, so for example lets say the list contains A and R
This is my code from before when the v.RentalStatus was just a string, can anyone tell me how i can modify this to work.
var statusStringList = rentalStatus.ToList().ConvertAll<string>(st => st.GetStringValue());
return from v in qry
where statusStringList.Contains(v.RentalStatus)
select v;
If it helps this is part of my query which returns the RentalStatus - its part of a group query but the RentalStatus is not in the group by
RentalStatus= g1.Select( j => j.IdRentalStatus).Distinct(),
g1 is my group by, so if you imagine there are 10 "A", 5 "U" .. then it would return an ienumerable of A and U ... as i am using Distinct. Not 10 As and 5 Us
I hope i have explained it well, please tell me if i haven't
I would appreciate any help from anyone ..
thanks
EDIT
This is my extension signature but not that it matters.
public static IQueryable<Rentals> WithStatus(this IQueryable<Rentals> qry, IList<Contants.Statuses> rentalStatus)
{
EDIT
As mentioned previously when v.RentalStatus was a string it was working but now its IEnumerable - hence a collection.. and it errors with this
Argument '1': cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'string'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果
RentalStatus
已从字符串更改为IEnumerable
那么您的比较 2 列表...我认为这应该有效:这应该为您提供任何具有您提供的列表中的状态
编辑:
是的,我会花一些时间学习 lambda 表达式。似乎它们被越来越多地使用并且有充分的理由。以下是一些教程链接:
对 LINQ 的广泛检查:Lambda 表达式和匿名类型
.NET Lambda 表达式 – 资源
这是真的吗?我认为 RentalStatuses 列表是您方法中的参数。我认为您的查询基本上可以让我获得状态与我指定的任何列表相匹配的所有租金。一个列表位于您的 Rental 对象上,另一个列表是我传入的列表...
至于为什么我的订单有效。我有一些问题:
你用它来查询数据库吗?你能看看它生成的tsql吗?
如果是这样,我会查看 tsql 并看看有什么区别。我得检查一下自己。我想我很幸运。
If
RentalStatus
has changed from a string to aIEnumerable<string>
then your comparing 2 list... I think this should work:This should give you any rentals that have a status that is in the list you are providing
Edit:
Yeah I would spend some time learn lambda expressions. Seems like they are being used more and more and with good reason. Here are a few links for tutorials:
An Extensive Examination of LINQ: Lambda Expressions and Anonymous Types
.NET Lambda Expressions – Resources
Is that true? I thought the list of rentalStatuses is a parameter in your method. I was thinking your query basically would allow me to get all the rentals that have a status that matches any of the list that I specified. One list lives on your Rental object and the other is the one I pass in...
As to why the order in mine worked. I have some questions:
Are you using this to query a database? Are you able to look at the tsql it generates?
If so, I would look at the tsql and see what the difference is. I would have to check myself. I got lucky I guess.
你可以尝试这样的事情:
You could try something like this:
我不确定,但我认为要在 Linq to SQL 中工作
Contains
,它必须是字符串(或整数或...)的数组,而不是任何 IEnumerable 。因此我会尝试:虽然可能还有其他问题,但我没有看那么多。
I am not sure but I think that for a
Contains
to work in Linq to SQL it must be an array of strings (or ints or ...) and not any IEnumerable. I would thus try:There might be other issues though, I did not look that much.
试试这个:
Try this: