Csharp/C#教程:通过linq从表中选择两列分享


通过linq从表中选择两列

我使用下面的查询来获取Entity Framework Linq中的所有列(20个以上)。 由于内存不足exception,我只想得到其中两个。 一个是“FileName”,另一个是“FilePath”。 如何修改我的代码?

var query = DBContext.Table1 .Where(c => c.FacilityID == facilityID && c.FilePath != null && c.TimeStationOffHook  c.FilePath) .Skip(1000) .Take(1000) .ToList(); foreach(var t in query) { Console.WriteLine(t.FilePath +"\"+t.FileName); } 

 var query = DBContext.Table1.Where(c => c.FacilityID == facilityID && c.FilePath != null && c.TimeStationOffHook < oldDate) .OrderBy(c => c.FilePath) .Skip(1000) .Take(1000) .Select(c => new { c.FilePath, c.FileName }) .ToList(); foreach(var t in query) { Console.WriteLine(t.FilePath +"\"+t.FileName); } 

你需要使用Select

只需选择其中两列:

 DBContext.Table1.Select(c => new { c.FileName, c.FilePath }); 

怎么样的

上述就是C#学习教程:通过linq从表中选择两列分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 using (var entity = new MyModel(ConnectionString)) { var query = (from myTable in entity.theTable where myTable.FacilityID == facilityID && myTable.FilePath != null && myTable.TimeStationOffHook < oldDate orderby myTable.FilePath select new { myTable,FileName, myTable.FilePath }).Skip(1000).Take(1000).ToList(); //do what you want with the query result here } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1004203.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐