imba22
imba22

Reputation: 649

linq query with a list in the where clause

I have a linq query written in the below format. Now when I am passing a consumerID it works fine and I am able to put it in the where clause. However when I try to pass a list of consumerIds, how can I put this in the where clause. I have looked through some solution online and they all use some other linq format which I dont want to use. Please suggesest if I can have a list in the where clause, something similar to how we can have in clause in sql??

public ICollection<ConsumerExchangeChangeDto> GetByConsumers(List<int> consumerIDs)
{
    EnrollmentReportingModel db = (EnrollmentReportingModel)Context.DbContext;
    var results = (from ecew in db.A
        join ecewe in db.B on ecew.ID 
        equals ecewe.ExchangeChangeEnrollmentWindowID into temp
        from j in temp.DefaultIfEmpty()
        join cecr in db.C on ecew.ConsumerExchangeChangeRequestID equals cecr.ID
        join con in db.D on cecr.ConsumerID equals con.ID
        where con.ID.Contains(consumerIDs) && !ecew.Deleted
        select new E
        {
            ConsumerID = con.ID,
            OrganizationID = con.OrganizationID,
            StartDate = ecew.StartDate,
            EndDate = ecew.EndDate,
            Deleted = ecew.Deleted
        }).ToList();
    return results;
}

Here is the dto class

public class E : ILzDto
{
    public int ConsumerID { get; set; }
    public int OrganizationID { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public bool Deleted { get; set; }
}

Upvotes: 0

Views: 941

Answers (1)

ChrisF
ChrisF

Reputation: 137108

Change this:

where con.ID.Contains(consumerIDs)

to this:

where consumerIDs.Contains(con.ID)

This will check if the id of the items in the select statement are in the input list.

Upvotes: 1

Related Questions