0

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?

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

dateRangeList = UnassignedWorkList.Select(x=> x.VendorFileDate between startdate and enddate).ToList();
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
D Embrey
  • 3
  • 3

2 Answers2

4

The .Select() method is for transforming results. To limit results, use 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 :

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). 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

Sam
  • 1,208
  • 1
  • 9
  • 16
0

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