简体   繁体   English

使用LINQ从日期范围中的列表中选择Distinct

[英]Select Distinct from list in date range with LINQ

I have a class for unassigned work. 我有一堂课未完成。

public class UnassignWork
{
    public int RecordNr { get; set; }
    public string GroupNum { get; set; }
    public string Section { get; set; }
    public string SubscriberID { get; set; }
    public decimal DedAmt { get; set; }
    public decimal CopayCoinsAmt { get; set; }
    public int BaseDed { get; set; }
    public int BaseOOP { get; set; }
    public string ClaimRespCode { get; set; }
    public string ClaimRejCode { get; set; }
    public DateTime VendorFileDate { get; set; }
    public string PackageCd { get; set; }
    public DateTime ClaimDOS { get; set; }
    public string WorkTypeCd { get; set; }
    public string AssignedTo { get; set; }
    public DateTime DateAssigned { get; set; }
}

I have a list based on the class.. 我有一个基于班级的名单。

List<UnassignWork> UnassignedWorkList = new List<UnassignWork>();`

How do I get a list of the items in UnassignedWorkList where the VendorFileDate is between startdate and enddate? 我如何在UnassignedWorkList中获取VendorFileDate在开始日期和结束日期之间的项目的列表?

List<UnassignWork> dateRangeList = new List<UnassignWork>();

dateRangeList = UnassignedWorkList.Select(x=> x.VendorFileDate between startdate and enddate).ToList();

The .Select() method is for transforming results. .Select()方法用于转换结果。 To limit results, use Where() 要限制结果,请使用Where()

You want something like : 您想要类似的东西:

UnassignedWorkList.Where(x => x.VendorFileDate > startData && x.VendorFileDate < endDate)

As noted by CodeNotFound in the comments here, You may actually want to use an inclusive range : 如CodeNotFound在此处的注释中所述,您实际上可能想要使用一个包含范围:

UnassignedWorkList.Where(x => x.VendorFileDate >= startData && x.VendorFileDate <= endDate)

To make the result distinct, you could use the Distinct() method, but that uses the default equality comparer so you'd need to implement the equality operator on your class for that to work ( see - https://msdn.microsoft.com/en-us/library/bb348436(v=vs.110).aspx ). 为了使结果与众不同,您可以使用Distinct()方法,但是该方法使用默认的相等比较器,因此您需要在类上实现相等运算符才能起作用(请参阅-https://msdn.microsoft。 com / zh-CN / library / bb348436(v = vs.110).aspx )。 You can implement a your own "DistinctBy" method or just use the GroupBy() as an alternative - you can see both approaches here : Distinct by property of class with LINQ 您可以实现自己的“ DistinctBy”方法,也可以仅使用GroupBy() -您可以在此处看到两种方法: 通过LINQ按类的属性进行区分

You can use following methods 您可以使用以下方法

class Program
{
    static void Main(string[] args)
    {
        var fromDate = DateTime.Today.AddDays(2);
        var toDate = DateTime.Today.AddDays(5);
        var tempList = new List<UnassignWork>();
        tempList.Add(new UnassignWork { DateAssigned=DateTime.Today.AddDays(1)});
        tempList.Add(new UnassignWork { DateAssigned = DateTime.Today.AddDays(1) });
        tempList.Add(new UnassignWork { DateAssigned = DateTime.Today.AddDays(2) });
        tempList.Add(new UnassignWork { DateAssigned = DateTime.Today.AddDays(3) });
        tempList.Add(new UnassignWork { DateAssigned = DateTime.Today.AddDays(4) });
        tempList.Add(new UnassignWork { DateAssigned = DateTime.Today.AddDays(5) });
        tempList.Add(new UnassignWork { DateAssigned = DateTime.Today.AddDays(6) });
        tempList.Add(new UnassignWork { DateAssigned = DateTime.Today.AddDays(7) });
        tempList.Add(new UnassignWork { DateAssigned = DateTime.Today.AddDays(8) });

        //Lambda Operation
        var filterdList = tempList.Where(e => e.DateAssigned >= fromDate && e.DateAssigned <= toDate);

        //Linq Operation
        var filterdList2 = (from t in tempList
                            where t.DateAssigned >= fromDate && t.DateAssigned <= toDate
                            select t);
    }
}
public class UnassignWork
{
    public int RecordNr { get; set; }
    public string GroupNum { get; set; }
    public string Section { get; set; }
    public string SubscriberID { get; set; }
    public decimal DedAmt { get; set; }
    public decimal CopayCoinsAmt { get; set; }
    public int BaseDed { get; set; }
    public int BaseOOP { get; set; }
    public string ClaimRespCode { get; set; }
    public string ClaimRejCode { get; set; }
    public DateTime VendorFileDate { get; set; }
    public string PackageCd { get; set; }
    public DateTime ClaimDOS { get; set; }
    public string WorkTypeCd { get; set; }
    public string AssignedTo { get; set; }
    public DateTime DateAssigned { get; set; }
}

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

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