Thunder
Thunder

Reputation: 11006

LINQ to SQL With the where with case statement

I have a SQL query that I want to convert into LINQ. I have pulled the data in to datatable.I am quite confused how to write the WHERE part of the query. The condition is dependent on a column in the table, If col_Con is "G" then use greater than on col_Val and 30(any value) else if col_Con is "L" then use less than condition on col_Val and 30(any value)

I am using Sqlserver 2005 for the SQL query part .

SELECT *
FROM   Mytable
WHERE 
    CASE
    WHEN col_Con= 'G' THEN 
        col_Val
    ELSE 
        30
    END 
        <= 
    CASE
    WHEN col_Con= = 'L' THEN 
        30
    ELSE 
        col_Val
    END 

Here is the start of my Linq

            //Mytable is a DataTable
            var drs = from DataRow dr in Mytable.Rows
            where   
            ...
            select dr;

Upvotes: 1

Views: 7923

Answers (1)

Guilherme Duarte
Guilherme Duarte

Reputation: 3441

var drs = from DataRow dr in Mytable.Rows
            where   
            (col_Con == 'G' ? col_Val : 30) <= (col_Con == 'L' ? 30 : col_Val)
            select dr;

Upvotes: 5

Related Questions