LINQ到SQL中使用case语句的where [英] LINQ to SQL With the where with case statement

查看:319
本文介绍了LINQ到SQL中使用case语句的where的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想转换为LINQ的SQL查询。我已经到datatable.I感到很困惑如何写在WHERE查询的一部分拉到数据。的条件是依赖于在表中的列,如果col_Con为G,则使用大于上col_Val和30(任何值)否则,如果col_Con为L,然后使用小于上col_Val和30(任何值)的条件

我使用sqlserver的2005年的SQL查询的一部分。

  SELECT *
FROM MYTABLE
哪里
    外壳
    WHEN col_Con ='G',那么
        col_Val
    其他
        三十
    结束
        < =
    外壳
    WHEN col_Con = ='L',那么
        三十
    其他
        col_Val
    结束
 

下面是我的LINQ

的开始

  // MYTABLE是一个DataTable
            DRS变种从DataRow的医生在Mytable.Rows =
            哪里
            ...
            选择医生;
 

解决方案

  VAR DRS =从DataRow的医生在Mytable.Rows
            哪里
            (col_Con =='G'col_Val:30?)< =(col_Con =='L'30:col_Val?)
            选择医生;
 

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;

解决方案

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

这篇关于LINQ到SQL中使用case语句的where的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆