2

我使用下面的查询来获取 Entity Framework Linq 中的所有列(20 多个)。由于内存不足异常,我只想获得其中两个。一个是“文件名”,另一个是“文件路径”。如何修改我的代码?

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

3 回答 3

11
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.

于 2013-02-06T13:51:36.337 回答
4

只需选择其中两列:

DBContext.Table1.Select(c => new { c.FileName, c.FilePath });
于 2013-02-06T13:51:50.313 回答
2

像这样的东西怎么样

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
            }
于 2013-02-06T13:58:53.100 回答