Thomas Andreè Wang
Thomas Andreè Wang

Reputation: 3429

How do i Query using mutiple strings in where statement in LINQ

I'm wondering how and if I can query on multiple strings/ints easily doing something like this?

var test = from a in db.PlasserLans
from b in db.Ulans
where a.registeredNick == b.Username &&
a.Nick == {"Ayuris" , "Crey" , "DjMofasa" , "esel" , "Firaxa" , 
           "Kindleguy" , "Michigo" , "moiC" ,"Shibiz"}
orderby a.Nick
select new
{
    Nick = a.Nick,
    Username = b.Username,
    Email = b.UserEMail,
    RealName = b.UserRealname,
};

Upvotes: 2

Views: 85

Answers (2)

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

You can create the list before the query, that would make it easier to understand. Also you can do a join instead of looping on both tables.

List<string> nickList=new List<string> {"Ayuris" , "Crey" , "DjMofasa" , "esel" , "Firaxa" , "Kindleguy" , "Michigo" , "moiC" ,"Shibiz"};

var test = from a in db.PlasserLans
           join b in db.Ulans
           on a.registeredNick equals b.Username
           where nickList.Contains(a.Nick)
           orderby a.Nick
           select new
                  {
                      Nick = a.Nick,
                      Username = b.Username,
                      Email = b.UserEMail,
                      RealName = b.UserRealname,
                  };

Upvotes: 3

Pleun
Pleun

Reputation: 8920

In Linq-2-sql its just the other way around

 where .... &&
 {"Ayuris" , "Crey" , "DjMofasa"}.Contains(a.Nick)

This is by heart, not sure if the direct usage of {} works, but you need to checkout the .Contains() for linq-2-sql and it will work.

Upvotes: 1

Related Questions