简体   繁体   English

如何使用linq获取Distinct项目

[英]How to get the Distinct item using linq

I am banging my head with some linq query logic. 我正在用一些linq查询逻辑来敲门。

I have a some list of work orders and i will count the items for weekly total. 我有一些工单清单,我将每周计算总数。

below is my code. 下面是我的代码。

var workorder = (
 from item in result1
 where 
 GetShortDate(item.Date)>=iStartDay
 &&
 GetShortDate(item.Date) < isunday 
 && item.WorkOrderId !=null
 group item by new { item.WorkOrderId, item.Count,item.Date } into resGroup
 select new {
     OrderId = resGroup.Key.WorkOrderId,
     Count = resGroup.Key.Count,
     Date = resGroup.Key.Date
  })
  .Distinct()
  .ToList();

var weeklycount = workorder.Sum(obj => obj.Count);

Hear my result1 input: 听到我的result1输入:

workorderId =1 , count = 8,date = 12/12/12
workorderId =1 , count = 8,date = 13/12/12
workorderId =2 , count = 1,date = 14/12/12

with the above logic i am getting weekly out put as 17 , but i need to get only 9 because my workorderId is same for two days. 按照上述逻辑,我每周的输出结果为17 ,但是我只需要得到9,因为我的workorderId在两天内都是相同的。 no need to care about date. 无需在意日期。

How can i achieve this.can some one guide me. 我怎样才能做到这一点。可以有人指导我吗。

Distinct will not work here, your objects are not distinct! Distinct在这里不起作用,您的对象也不独特!

You need to group by workorderId and (presumably) only include the first item in the sum 您需要按workorderId分组,并且(大概)仅将总和中的第一项包括在内

var workordersForWeek = (from item in allresult
                                 where 
                                 GetShortDate(item.Date)>=iStartDay
                                 &&
                                 GetShortDate(item.Date) < isunday 
                                 && item.WorkOrderId !=null //to eleminate weekly total, which will not have workorderid
                                 group item by new { item.WorkOrderId, item.Count,item.Date } into resGroup
                                 select new {
        workOrderId=resGroup.Key.WorkOrderId,
        Count=resGroup.Key.Count,
        Date=resGroup.Key.Date
        }).GroupBy(x => x.WorkOrderId).Sum(obj => obj.First().Count)

But as you can see, this now group's the result twice. 但是正如您所看到的,现在将结果分组两次。 This answer will probably do what you want, but there is probably an easier way which includes not grouping by date if you dont care about the date. 该答案可能会满足您的要求,但是如果您不关心日期,则可能有一种更简单的方法,其中包括不按日期分组。

You may want to try something along the lines of: 您可能需要尝试以下方法:

var workordersForWeek = allresult.Where(item => GetShortDate(item.Date) >= iStartDay 
                                              && GetShortDate(item.Date) < isunday 
                                              && item.WorkOrderId != null)
                                 .GroupBy(item => item.WorkOrderId, x => x.Count);

var weeklycount = workordersForWeek.Sum(x => x.First());

Untested, so not 100% sure on that. 未经测试,因此不能100%确定。

Live example: http://rextester.com/NLC52773 实时示例: http//rextester.com/NLC52773

By looking at this part of your query group item by new { item.WorkOrderId, item.Count,item.Date } WorkOrderId 1 has two different dates hence its appearing twice and then you count is being added twice. 通过按新{{item.WorkOrderId,item.Count,item.Date}来查看查询组项目的这一部分,WorkOrderId 1有两个不同的日期,因此它出现两次,然后您的计数被添加两次。 Remove the item.Date from the grouping. 从分组中删除项目。

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

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