如何将此 SQL 转换为 ASP.NET MVC 的 LINQ?

我有这个 SQL 语句:


select 

    table1.DIRECTORATE_ID, 

    table2.PLA_NAME + '//' + table1.ENGLISH_DIRECTORATE_CODE as EngCode

from table1

inner join table2 on table1.PLA_ID = table2.PLA_ID order by EngCode

我需要在我的 EF 代码中执行相同类型的查询,这会给我一个List<>. 像这样的东西:


var query = (from t1 table1.DIRECTORATE_ID, table2.PLA_NAME + '//' + table1.ENGLISH_DIRECTORATE_CODE as EngCode

             from table1

             inner join table2 on table1.PLA_ID = table2.PLA_ID order by EngCode)

            .ToList();


弑天下
浏览 145回答 1
1回答

MMTTMM

您的from子句中有列引用。在 SQL 中,顺序是(在这个特定的例子中)选择从加入订购在LINQ中,顺序是从加入订购单选择您可以在查询语法中编写此内容var query = from t1 in table1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; join t2 in table2 on t1.PLA_ID equals t2.PLA_ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let EngCode = t2.PLA_NAME + "//" + t1.ENGLISH_DIRECTORATE_CODE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderby EngCode&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select new&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t1.DIRECTORATE_ID,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EngCode&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }var data = query.ToList();或者使用扩展方法var data = table1&nbsp; &nbsp; .Join(&nbsp; &nbsp; &nbsp; &nbsp; table2,&nbsp; &nbsp; &nbsp; &nbsp; t1 => t1.PLA_ID,&nbsp; &nbsp; &nbsp; &nbsp; t2 => t2.PLA_ID,&nbsp; &nbsp; &nbsp; &nbsp; (t1, t2) => new&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t1.DIRECTORATE_ID,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EngCode = t2.PLA_NAME + "//" + t1.ENGLISH_DIRECTORATE_CODE&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; .OrderBy(x => x.EngCode)&nbsp; &nbsp; .ToList();
打开App,查看更多内容
随时随地看视频慕课网APP