简体   繁体   中英

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. Below sql statement working fine as per my requirement. But I need to do same using LINQ. Please suggest how can I achieve same using LINQ? or Equivalent LINQ statement?

 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. It has to be 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??"" })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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