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])
---
应该满足你的要求!