Home > Blockchain >  Linq query for condition on one property for fetching record
Linq query for condition on one property for fetching record

Time:12-09

var Data = // selecting some cloumns from postgresql using linq query

Data contain all propery of class A (Student Name,Subject Name & MarksObtained)

Data has following value

Student Name   Subject Name       MarksObtained
Rani           Maths              43
Rani           Physics            30
Manasa         Maths              39
Manasa         Physics            45
Aisharvya      Physics            47

Now I want to do further modification existing record. When Subject is Maths and MarksObtained is greater than 40,should not appear/take that record.

Output like

 Student Name   Subject Name       MarksObtained
    Rani           Physics            30
    Manasa         Maths              39
    Manasa         Physics            45
    Aisharvya      Physics            47

How to do that?

CodePudding user response:

var Data = YourList.Where(x => !(x.SubjectName == "Maths" && x.MarksObtained > 40));

Explanation: when you put something inside ( ), all the stuff in there are considered as one statement. And the ! suggests Not to the statement(s) inside ( ) (if they are Boolean expression, which they are in this case)

As jmcilhinney said in the comments, you must always try to figure out your problem on your own, not because of "carry your own burden", but because that's the only way you can learn. I'm only answering because I need some reputation points

I would appreciate it if you could accept the answer

CodePudding user response:

Try something like this:

var list2 = list.Where(a => !string.Equals(a.subjectName, "Maths") || a.marksObtained < 40)
                .ToList();
  • Related