簡體   English   中英

Linq查詢到SQL查詢

[英]Linq query to SQL query

我有一個運行良好的Linq查詢,但是我需要編寫SQL查詢。有人可以幫我寫嗎? 該查詢搜索數據庫foreach aha.HVview的時間和模式的過濾器,並最終它會檢查選項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查詢的轉換后的代碼

  1. 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(e
    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(e
    在此處輸入代碼pn, 15, 2) = a.HV)

認為用您的真實值'Filter.To'替換引用的變量...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM