C# Linq多表查詢多引數排序
阿新 • • 發佈:2019-06-22
SQL to Linq
SQL:
strQuery = "SELECT A.*, B.LABEL_DESC FROM LBLMATREL A, LBLDEF B" + " WHERE A.FACTORY = B.FACTORY" + " AND A.LABEL_ID = B.LABEL_ID" + " AND A.FACTORY=?" + " AND A.MAT_ID=?" + " AND A.MAT_VER=?" + " AND A.LABEL_ID>=?" + " ORDER BY A.FACTORY ASC" + " , A.MAT_ID ASC" + " , A.LABEL_ID ASC";
Linq語句:
var listResult=ctx.Lblmatrel.Where(t => t.Factory == sFactory && t.MatId == sMatId && t.MatVer == iMatVer && t.LabelId.CompareTo(sLabelId) >= 0).Join( ctx.Lbldef, a => new { f = a.Factory, id = a.LabelId }, b => new { f = b.Factory, id = b.LabelId }, (a, b) => new { a, b.LabelDesc } ).OrderBy(t => t.a.Factory).ThenBy(t => t.a.MatId).ThenBy(t => t.a.LabelId).ToList();
注:接收變數型別不能是List,因為查詢結果不是單