C# 带有 linq lambda 表达式的查询列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17330155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:11:49  来源:igfitidea点击:

query list with linq lambda expressions

c#linq

提问by user1202606

How would I get participants that are in a list of counties? I get the counties in var counties, then I want to get all of the participants that have a CountyOfParticipationId that is in the list of counties.

我如何获得县列表中的参与者?我得到了 var 县中的县,然后我想得到所有在县列表中拥有 CountyOfParticipationId 的参与者。

if (collaborationId != null)
{
    var counties = (from c in db.CountyCollaborations
                    where c.CollaborationId == collaborationId
                    select c).ToList();
    participants = participants.Where(p => p.CountyOfParticipationId in counties);


}

采纳答案by jods

.Where(p => counties.Contains(p.CountyOfParticipationId))

.Where(p => counties.Contains(p.CountyOfParticipationId))

Now if there's a lot of data be careful with the complexity of this. Containsin a list is O(n), so overall the algorithm is O(n*m) with n,m being the # of participants and the # of counties.

现在,如果有大量数据,请注意其复杂性。Contains在一个列表中是 O(n),所以整个算法是 O(n*m),其中 n,m 是参与者的数量和县的数量。

For better performance you could store the counties in a HashSet, which has O(1) Contains, meaning O(n) overall.

为了获得更好的性能,您可以将县存储在一个 HashSet 中,它具有 O(1) 包含,这意味着总体上是 O(n)。

Of course if the number of counties is small it doesn't matter, really.

当然,如果县的数量很少,那也没关系,真的。

EDIT: Just noted that your list doesn't contain the ids but full objects. For the code above to work you also need to change your linq query from select cto select c.Idor something like that (don't know the name of the field).

编辑:刚刚注意到您的列表不包含 ID,而是完整的对象。要使上面的代码正常工作,您还需要将 linq 查询从 更改select cselect c.Id或类似的内容(不知道字段的名称)。

回答by AD.Net

participants = participants
.Where(p => counties.Any(c=> c.CountyId == p.CountyOfParticipationId) )

Or

或者

participants.Where(p => p.County.CollaborationId == collaborationId)

should also work if you have set up relations properly

如果您已正确建立关系,也应该可以工作

回答by AaronLS

Assuming each county has a CountyId:

假设每个县都有一个 CountyId:

participants = participants.Where( p => 
  counties.Select(c=> c.CountyId ).Contains( p.CountyOfParticipationId) );

回答by konkked

This might be better in some situations since you won't have to store counties separately if the linq method is translating the expression to sql behind the scences.

这在某些情况下可能会更好,因为如果 linq 方法在后台将表达式转换为 sql,您就不必单独存储县。

participants = (from p in participants 
                  join c in 
                      db.CountyCollaborations
                          .Where(cty=>cty.CollaborationId == collaborationId)
                      on p.CountyOfParticipationId equals c.CountyId
                select p);