Deepan Raj
Deepan Raj

Reputation: 314

how to avoid two columns contain 0 in linq

assume as i have to shown the list of value in the screen

iam taking the list using linq

    Student     English      Hindi     Tamil      MArathi
    -------    ---------    -------   -------    ---------

    Deepan      56           65          34         45

    Mohan       45           34          0          23

    Murali      56           89          0          0

Assume that i have these values in db.....

I dont want to show if (tamil and marathi ) is 0.....if both r contains 0 means .... i have to avoid that row while taking from database using linq ...eg(murali)...but i dont want to avoid Mohan.....pls give me the linq query

Now i tried this

   var ulist = (from c in CustomerTransactions
              where c.TransTypeID==12
                         select new
                         {
                             Student=c.Student,

                             English=c.English,
                             Hindi=c.Hindi,
                             Tamil=c.Tamil,
                             Marathi=c.Marathi
                         }).ToList().OrderBy(b => b.Student).Where(x => x.Marathi!=0 );

Upvotes: 0

Views: 152

Answers (2)

sachin
sachin

Reputation: 2361

This is what you can use if Lambda is fine for you. You can combine all the 3 checks in first Where clause itself, instead of specifying the conditions at 2 different places.

    CustomerTransactions.Where(c => c.TransTypeID==12 && !(c.Marathi == 0 && c.Tamil == 0))
                         .OrderBy(c => c.Student)
                         .Select(c => new {
                             Student=c.Student,
                             English=c.English,
                             Hindi=c.Hindi,
                             Tamil=c.Tamil,
                             Marathi=c.Marathi
                         }).ToList();

Upvotes: 0

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

You can combine several conditons in a single where-statement:

.Where(x => x.Marathi !=0 && x.Tamil != 0)

This will select only those entries where both conditions pass (meaning both Marathi and Tamil are not 0).

Upvotes: 1

Related Questions