本文介绍了将2个Linq查询合并为1个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下信息,我如何将这2个linq查询合并为1个.join语句有点麻烦.

Given the following information, how can I combine these 2 linq queries into 1. Having a bit of trouble with the join statement.

'projectDetails'只是ProjectDetails的列表
ProjectDetails(1到很多)PCardAuthorizations
ProjectDetails(1到很多)ExpenditureDetails

'projectDetails' is just a list of ProjectDetails
ProjectDetails (1 to many) PCardAuthorizations
ProjectDetails (1 to many) ExpenditureDetails

注意,我正在按相同信息分组并选择相同类型的信息

Notice I am grouping by the same information and selecting the same type of information

var pCardAccount =  from c in PCardAuthorizations
                    where projectDetails.Contains(c.ProjectDetail)
                        && c.RequestStatusId == 2
                    group c by new { c.ProjectDetail, c.ProgramFund } into g

                    select new { Key = g.Key, Sum = g.Sum(x => x.Amount) };



var expenditures =  from d in ExpenditureDetails
                    where projectDetails.Contains(d.ProjectDetails)
                        && d.Expenditures.ExpenditureTypeEnum == 0
                    group d by new { d.ProjectDetails, d.ProgramFunds } into g
                    select new {
                        Key = g.Key,
                        Sum = g.Sum(y => y.ExpenditureAmounts.FirstOrDefault(a => a.IsCurrent && !a.RequiresAudit).CommittedMonthlyRecords.ProjectedEac)
                    };

推荐答案

我认为Mike的建议不会在这里起作用.这两个查询有很大的不同,因此将它们组合成一个查询只会增加阅读难度.

I don't think Mike's suggestion will work here. These two queries are sufficiently different that combining them into one query will just make it more difficult to read.

  • 对象具有不同的类型.
  • where子句不同.
  • group by子句不同:
    • 新{c.ProjectDetail,c.ProgramFund}
    • 新{{c.ProjectDetails,c.ProgramFunds}

    事实上,实际上没有任何相同之处.我建议保持原样.

    In fact there isn't really anything that is the same. I'd recommend leaving it as it is.

    这篇关于将2个Linq查询合并为1个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 17:10