[英]Linq query to SQL query
我有一个运行良好的Linq查询,但是我需要编写SQL查询。有人可以帮我写吗? 该查询搜索数据库foreach
ah
和a.HV
在view
的时间和模式的过滤器,并最终它会检查选项Filter.M
,如果选择了它,它会搜索在这个DropDownCheckBoxes`选择的所有数据我该如何写在哪里并选择SQL命令中的一部分?
ret1 = (from a in View
where
a.LastRefreshTime>=Filter.From && a.LastRefreshTime<=Filter.To && a.ModelCode == mdlCode &&
Filter.PN.Select(epn => epn.Substring(0, 11)).Contains(a.H) &&
Filter.PN.Select(epn => epn.Substring(14, 2)).Contains(a.HV)
select new RData
{
v = a.v,
Date = a.LastRefreshTime,
UserId = a.UserId,
M = a.Name,
}).Distinct().AsQueryable();
ret = ret1.Where(nr =>
Filter.M == null || !Filter.M.Any() || Filter.M.Contains(nr.M)
).ToList();
这是您的起点
select a.v v,
a.LastRefreshTime "Date",
a.UserId,
a.Name
from a
where a.LastRefreshTime>= arg_filter_from
and a.LastRefreshTime<= arg_filter_to
and a.ModelCode = arg_mdlCode
.
.
.
在此查询中,您需要将“ arg _...”替换为所需的适当值或参数。
在SQL中, 包含大致相当于“ IN” 。 例如:
where a.Name in ('jim', 'bob', 'joe')
In也可以与子选择一起使用,虽然我不是linq专家,但它大致是我认为Filter.PN.Select正在执行的操作。 例:
where a.H in (Select foo from PN_Table)
或更简单的示例继续我以前的名称示例:
where a.Name in (select first_name from table)
如果我们假设Filter.PN列表表示您的sql数据库中的表FilterPN,则它将是您对第一个linq查询的转换后的代码
select distinct av, a.LastRefreshTime, a.UserId, a.Name
from [view] a where a.LastRefreshTime>= 'Filter.From' and
a.LastRefreshTime<='Filter.To' and a.ModelCode = 'mdlCode' and
exists(select top 1 * from FilterPN where Substring(epn, 1, 11) = aH) and exists(select top 1 * from FilterPN where Substring(eselect distinct av, a.LastRefreshTime, a.UserId, a.Name
在此处输入代码
from [view] a where a.LastRefreshTime>= 'Filter.From' and
a.LastRefreshTime<='Filter.To' and a.ModelCode = 'mdlCode' and
exists(select top 1 * from FilterPN where Substring(epn, 1, 11) = aH) and exists(select top 1 * from FilterPN where Substring(epn, 15, 2) = a.HV)
认为用您的真实值'Filter.To'替换引用的变量...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.