使用LINQ

时间:2018-07-02 21:22:20

标签: c# linq

我有一个未完成的课程。

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; }
}

我有一个基于班级的清单。

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

如何在UnassignedWorkList中获得VendorFileDate在开始日期和结束日期之间的项目的列表?

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

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

2 个答案:

答案 0 :(得分:4)

.Select()方法用于转换结果。 要限制结果,请使用Where()

您想要类似的东西:

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

如CodeNotFound在此处的注释中所述,您实际上可能希望使用一个包含范围:

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

要使结果与众不同,可以使用Distinct()方法,但是该方法使用默认的相等比较器,因此您需要在类上实现相等运算符才能起作用(请参阅-{{3 }})。您可以实现自己的“ DistinctBy”方法,也可以仅使用GroupBy()-您可以在此处看到两种方法:https://msdn.microsoft.com/en-us/library/bb348436(v=vs.110).aspx

答案 1 :(得分:0)

您可以使用以下方法

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; }
}