linq比较2列表

发布于 2024-12-03 09:14:13 字数 1109 浏览 1 评论 0原文

var companytoexclude = from c in db.Companies
                   from p in c.Projects
                   where p.ProjectEndDate == null
                   select c;

var list = from m in db.Members.Include("Companies.Projects.Experiences")
                     where m.MemberId == id
                     select m;

如何排除在 companytoexclude 列表中的公司?

会员有多家公司
公司有很多项目
项目有很多经验



K我解决了它。 所以我所做的只是添加,

   foreach (Company c in companytoexclude )
            {
                list .First().Companies.Remove(c);
            }
            

所以有更容易显示的吗? 我想要具有公司列表的成员,而不仅仅是公司。

现在一些数据

Member
MemberId    Name
2011        Jet

Company
CompanyId   MemberId    CompanyName
18          2011            Company1
29          2011            Company2

ProjectId   CompanyId   ProjectName ProjectEndDate
1           29          Project1    2008-08-01 00:00:00.000
2           29          Project2    2009-08-01 00:00:00.000
3           18          Project3        NULL

我希望它返回除company1和project3之外的所有数据

var companytoexclude = from c in db.Companies
                   from p in c.Projects
                   where p.ProjectEndDate == null
                   select c;

var list = from m in db.Members.Include("Companies.Projects.Experiences")
                     where m.MemberId == id
                     select m;

how do I exclude companies that are in companytoexclude in list?

Member have many company
company have many project
project have many experiences

K I kind of solved it.
So what I did is just add in

   foreach (Company c in companytoexclude )
            {
                list .First().Companies.Remove(c);
            }
            

so is there a easier to show this?
I want the member with list of companies, not just companies.

some data

Member
MemberId    Name
2011        Jet

Company
CompanyId   MemberId    CompanyName
18          2011            Company1
29          2011            Company2

ProjectId   CompanyId   ProjectName ProjectEndDate
1           29          Project1    2008-08-01 00:00:00.000
2           29          Project2    2009-08-01 00:00:00.000
3           18          Project3        NULL

now I want it to return everything but company1 and project3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无语# 2024-12-10 09:14:13

怎么样:

var list = from m in db.Members.Include("Companies.Projects.Experiences")
           where m.MemberId == id
           where !m.Companies.Intersect(companiesToExclude).Any()
           select m;

这只会返回不包含您试图排除的任何公司的成员。

请注意,鉴于您指定了 MemberId,无论如何我几乎只期望一个结果......目前还不清楚您要做什么。

如果您希望排除该单个成员中除公司之外的所有公司,很简单:

var member = db.Members.Include("Companies.Projects.Experiences")
               .Single(m => m.MemberId == id);
var companies = member.Companies.Except(companiesToExclude);

如果这不是您想要的,请澄清您的问题。

另请注意,您的 companiesToExclude 查询一开始可能并不正确。您是否试图排除所有拥有结束日期为空的任何项目的公司?如果是这样,您想要:

var companiesToExclude = db.Companies
                           .Where(c => c.Projects
                                        .Any(p => p.ProjectEndDate == null))

How about:

var list = from m in db.Members.Include("Companies.Projects.Experiences")
           where m.MemberId == id
           where !m.Companies.Intersect(companiesToExclude).Any()
           select m;

That will only return members which don't include any companies which you're trying to exclude.

Mind you, given that you're specifying a MemberId, I'd pretty much only expect one result anyway... it's unclear what you're trying to do.

If you want all the companies from that single member except the companies to exclude, it's easy:

var member = db.Members.Include("Companies.Projects.Experiences")
               .Single(m => m.MemberId == id);
var companies = member.Companies.Except(companiesToExclude);

If that's not what you're after, please clarify your question.

Also note that your companiesToExclude query may well not be right to start with. Are you trying to exclude all companies with any project with a null end date? If so, you want:

var companiesToExclude = db.Companies
                           .Where(c => c.Projects
                                        .Any(p => p.ProjectEndDate == null))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文