简体   繁体   English

SQL语句的等效LINQ语句

[英]Equivalent LINQ statement to SQL statement

I have a table with five columns. 我有一个五列的表。 Only one of first four column will have a value and other will be null at a time and I need to get that non-null value from the table row with check on fifth column. 前四列中只有一列会有一个值,而其他一次只能为null,我需要从表行中获取非空值并检查第五列。 Below sql statement working fine as per my requirement. 根据我的要求,在sql语句下工作正常。 But I need to do same using LINQ. 但我需要使用LINQ做同样的事情。 Please suggest how can I achieve same using LINQ? 请建议如何使用LINQ实现相同目的? or Equivalent LINQ statement? 或等效的LINQ声明?

 SELECT 
   CASE 
      WHEN col1 is not null THEN col1 
      WHEN col2 is not null  THEN col2 
      WHEN col3 is not null  THEN col3 
      WHEN col4 is not null  THEN col4 
   END 
 FROM MyTable where col5 ='abc'

First thing first. 首先是第一件事。 Your sql is not correct. 你的sql不正确。 It has to be SELECT COALESCE(col1,col2,col3,col4) AS COL FROM MyTable where col5 ='abc' . 它必须是SELECT COALESCE(col1,col2,col3,col4) AS COL FROM MyTable where col5 ='abc'

var myObj = db.MyTable.Where(s => s.Col5 == 'abc').Select(s=> new {COL = s.col1??s.col2??s.col3??s.col4??"" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM