在LINQ的where子句中如何使用case语句?

huangapple go评论63阅读模式
英文:

How to use case statement in linq where clause

问题

表格:Id,Name,Subject,Score

1 Pinky English 60

2 Pinky Maths 40

3 Mahesh Maths 50

4 Rani Maths 50

5 Rani Eco 50

我想从表格中获取记录,当科目是数学时,需要记录的分数大于50,对于其他科目没有条件。

输出:

1 Pinky English 60

3 Mahesh Maths 50

4 Rani Maths 50

5 Rani Eco 50

查询:

(from tbl in dbContext.Table1
where tbl.Subject = 'Maths'
select tbl)

如何在这里的where子句中添加条件。

(from tbl in dbContext.Table1 where tbl.Subject = 'Maths' select tbl)

英文:

Table : Id, Name,Subject,Score

1 Pinky English 60

2 Pinky Maths 40

3 Mahesh Maths 50

4 Rani  Maths 50

5 Rani  Eco 50

I want to fetch record from table ,when Subject is maths, need record whose score should be more than 50,for remaining subject no condition.

Output :

1 Pinky English 60

3 Mahesh Maths 50

4 Rani  Maths 50

5 Rani  Eco 50

Query :

(from tbl in dbContext.Table1
where tbl.Subject ='Maths'
select tbl)

How to add that condition in where clause here.

(from tbl in dbContext.Table1 where tbl.Subject ='Maths' select tbl)

答案1

得分: 1

由于问题是开放的,我将把我的评论作为答案添加。

var r = dbContext.Table1
          .Where(x => x.Subject == "Maths" && x.Score >= 50 || x.Subject != "Maths");

这段代码是C#代码,它使用Entity Framework进行数据库查询。它的作用是从名为"Table1"的数据库表中选择满足以下条件的记录:科目为"Maths"且分数大于等于50,或者科目不是"Maths"。

英文:

Since the question is open I’ll add my comment as an answer.

var r = dbContext.Table1
          .Where(x => x.Subject == "Maths" && x.Score >= 50 || x.Subject != "Maths");

huangapple
  • 本文由 发表于 2023年8月8日 20:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定