English 中文(简体)
ef4 组,按导航条件排列和总和
原标题:ef4 group by and sum with condition on navigation

我有两张桌子

**table 1**
int tid
money money
datetime date
int serviceid
int type


**table2**
int id
int tid
status nvarchr()

我使用Ef4,我拥有两个实体的导航财产

i 试图在日期前得到钱财集团的金额 [我也做了一个新等级 具有这种属性]

Total money
Total money of type [1]
total money where table2 status is a or b 

how can I achieve that king of query THIS IS WHAT I GOT SO FAR :

return db.TBL1.Include("TBL2").Where(x => x.date >= startDate && x.date <= endDate && x.ServiceID == sid).GroupBy(e => new { e.Date.Value.Year, e.Date.Value.Month, e.Date.Value.Day }).AsEnumerable().Select(group => new entity c (Convert.ToInt32(group.Sum(x => x.Money)), Convert.ToInt32(group.Where(d => d.BillingType == 1).Sum(x => x.Money)),********what can i do here*****, Convert.ToDateTime(group.FirstOrDefault().Date))).AsEnumerable<billingReport>().ToList();
问题回答
return db.TBL1
         .Where(x => x.date >= startDate && 
                     x.date <= endDate && 
                     x.ServiceID == sid)
         .GroupBy(e => new 
         { 
             e.Date.Value.Year, 
             e.Date.Value.Month, 
             e.Date.Value.Day 
         })
         .Select(group => new billingReport
         {
             TotalMoney = Convert.ToInt32(group.Sum(x => x.Money), 
             TotalMoneyOfType1 = Convert.ToInt32(group.Where(d => d.BillingType == 1)
                                                      .Sum(x => x.Money)),
             TotalMoneyForStatus = group.Where(x=>x.TBL2.Any(y => y.Status == "a" || 
                                                                  y.Status == "b"))
                                        .Sum(x => x.Money), 
             Date = Convert.ToDateTime(group.FirstOrDefault().Date))
         })
         .ToList();




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签