通过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 }); 

怎么样的

 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 }