英文:
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");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论