user3587624
user3587624

Reputation: 1471

Add undetermined number of AND on Linq-SQL Query?

Let's say that I have the following function...

private List<QueryObject> ReturnSQLData (DateTime d1, DateTime d2, string[] arrayOfNames)
{
    var results = (from a in _context.Names
                   where (a.CreatedOn >= d1 && a.CreatedOn <d2)

                   select new QueryObject
                   {
                       FirstName = a.Name
                       LastName = a.LastName
                   }).ToList();

    return results;
}

How could I continue create a Linq query where I have something line this?

private List<QueryObject> ReturnSQLData (DateTime d1, DateTime d2, string[] arrayOfNames)
{
    var results = (from a in _context.Names
                   where (a.CreatedOn >= d1 && a.CreatedOn <d2)
                   && (arrayOfNames[0].Equals("abc") || arrayOfNames[1].Equals("cde")
                   select new QueryObject
                   {
                       FirstName = a.Name
                       LastName = a.LastName
                   }).ToList();

    return results;
}

Note that I don't really know the size of the string[] until I am actually executing the code. How could I then add this statement on my linq query?

> && (arrayOfNames[0].Equals("abc") || arrayOfNames[1].Equals("cde") ||
> ... || arrayOfNames[n].Equals("cde"))

Would that be possible?

BTW. I am running this on a ASP.NET 5 MVC6 EF7 solution in case it helps :)

Thanks!

Upvotes: 2

Views: 43

Answers (1)

John Boker
John Boker

Reputation: 83709

can you do:

&& arrayOfNames.Contains("cde")

Upvotes: 4

Related Questions