简体   繁体   English

c#Linq Object subQuery / In

[英]c# Linq Object subQuery / In

I have 2 objects that contains generic list properties. 我有2个包含通用列表属性的对象。 IE : IE:

public class User
{
    public string Sid { get; set; }
    public List<Group> Groups { get; set; }
}

public class Section
{
    public string Sid { get; set; }
    public List<Group> Groups { get; set; }
}

From my BLL I get a generic list of sections List mySections=SectionDB.getList(); 从我的BLL我得到一个通用的List ListSeSections = SectionDB.getList();

and my User object contains the user Information User myUser=UserDB.getInfo(sid); 我的用户对象包含用户信息用户myUser = UserDB.getInfo(sid);

Using linq to objects, is it possible to make a query that retreives all the sections where there is at least one group within the groups user class? 使用linq到对象,是否可以创建一个查询来检索组用户类中至少有一个组的所有部分?

Any help? 有帮助吗?

from section in mySections
from sectionGroup in section.Groups
where myUser.Groups.Any(userGroup => userGroup == sectionGroup)
select section

i'd rather go for any, as you use the iterator much more efficiently 我宁愿去任何,因为你更有效地使用迭代器

var sections = mySections.Where(x => x.Groups.Intersect(myUser.Groups)
    .Any()).ToList();

(note that this relies on either referential equality of the Group instances, or a suitable Equals / GetHashCode implementation on the Group type) (请注意,这依赖于Group实例的引用相等性,或者Group类型上合适的Equals / GetHashCode实现)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM