當前位置:首頁 » 編程語言 » linq語句轉sql
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

linq語句轉sql

發布時間: 2022-02-15 14:57:20

A. 如何把下面的執行查詢的sql語句轉化為linq to sql語句

我在SQL裡面測試了一下,@sql生成的sql語句是

selectYearMonth,[A]=sum(caseCodeAwhen'A'thenNumberelseNULLend),[B]=sum(caseCodeAwhen'B'thenNumberelseNULLend),[C]=sum(caseCodeAwhen'C'thenNumberelseNULLend)fromagroupbyYearMonth

而執行結果是

B. sql 語句轉成Linq to sql

說實話,你的sql似乎有問題。是否可以理解a與c都是outer表?
如果就按字面的要求,就是下面的:
var v = from c in EnterpriseInformation
from b in StateTax
from a in LandTax
where c.TaxCode==b.textCode || a.TaxCode ==c.TaxCode
select c;
但這個效率一定很低,可以改一下邏輯:
var v = (from c in EnterpriseInformation
join b in StateTax on c.TaxCode == b.textCode
into joinedtable
from g in joinedtable.DefaultIfEmpty()
select c)
.Union(
from c in EnterpriseInformation
join a in LandTax on a.TaxCode == c.TaxCode
into joinedtable
from g in joinedtable.DefaultIfEmpty()
select c);

C. sql 語句轉linq語句

int? temp;
var result=from p in tb_aaa
where p.title.Substring(0,8)=="(2012)年第" && int.Tryparse(p.title.Substring(8,2),temp) && p.title.Substring(10,1) =="號"
select p;

第一個表達式和第三個表達式不說了,主要是第二個,判斷它是否可以轉為數字,用到了tryparse,所以需要一個外部的int來配合,純手寫,可能有寫大小寫或語法錯誤

D. 怎麼把SQL 語句轉換成 linq 語句,會的來,不會的別扯犢子,謝謝!!

varq=fromaindb.Jobjoinbindb.Departmentona.Dp_IDequalsb.ID
where(fromcindb.Departmentwherec.Parent_ID==35selectc.id).Contains(b.Parent_ID)||b.Parent_ID==35||b.ID==35
selectnew{Job=a,Department=b};

E. sql語句轉換成linq,在線等!!!

from a in db.Procts
where
((from t in db.CustomerContractItems
where
t.IsMerge == "false" &&
t.Deleted == null &&
t.ProctID == a.Id
select new {
t.ProctID
}).Distinct()).FirstOrDefault().!= null
group a by new {
a.ProctCategoryID
} into g
select new {
ProctCategoryID = (Int16?)g.Key.ProctCategoryID,
pcoun = (Int64?)g.Count()

親,寫出不容易,求速度採納

F. SQL轉化成LINQ語句

(
fromcinClass
orderby(c.Fraction=="0"?1:0),c.Fractionascending
selectnew{
c.FID,
c.Student,
c.Fraction,
c.Tesct
}
).Take(1000)

G. sql語句轉換成相應的linq語句

這樣子:
var q = (
from o in upt_organizationstruct
select o
).Except(
from p in uos_parentlevel
where p.uos_parentlevel <> null
select p
);
foreach (var result in q)
{
......
}

H. SQL語句轉換成Linq!

太長的sql轉成linq也是比較麻煩的,有時候有點得不償失的感覺,如果你用了linq to ef,且表之間外鍵關系已經在model里了,還可以一試,否則你還不如建個視圖,直接像表一樣訪問

I. linq語句轉換成sql語句

linq語句呢?

J. SQL語句轉化為Linq語句

分少點!回頭幫我的團隊打工償還吧!O(∩_∩)O哈哈~
-------------
我的表是Table_1s和Table_2s
linq 查詢如下:
from t in Table_1s
join f in Table_2s on t.B equals f.Key into FD
from f in FD.Where(b=>b.RowName=="B").DefaultIfEmpty()
join s in Table_2s on t.C equals s.Key into FDS
from s in FDS.Where(b=>b.RowName=="C").DefaultIfEmpty()
select new
{
A=t.A,
B=f.Value,
C=s.Value
}
---
對應sql如下:
-- Region Parameters
DECLARE @p0 NVarChar(1) SET @p0 = 'B'
DECLARE @p1 NVarChar(1) SET @p1 = 'C'
-- EndRegion
SELECT [t0].[A], [t1].[value] AS [B], [t2].[value] AS [C]
FROM [Table_1] AS [t0]
LEFT OUTER JOIN [Table_2] AS [t1] ON ([t1].[RowName] = @p0) AND ([t0].[B] = [t1].[key])
LEFT OUTER JOIN [Table_2] AS [t2] ON ([t2].[RowName] = @p1) AND ([t0].[C] = [t2].[key])
---
應該滿足你的要求!