通过LINQ从表中选择两列 [英] Select two columns from the table via linq

查看:177
本文介绍了通过LINQ从表中选择两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的查询得到实体框架LINQ的所有列(20个)。由于内存溢出异常的,我只想让他们两个。一个是文件名,另一种是文件路径。如何修改我的代码



  VAR的查询= DBContext.Table1 
。凡(C => c.FacilityID == facilityID&放大器;&安培; c.FilePath = NULL&放大器;!&安培; c.TimeStationOffHook< oldDate)
.OrderBy(C => c.FilePath)
.Skip(1000)
。取(1000)
.ToList();
的foreach(查询变种T)
{
Console.WriteLine(t.FilePath +\\+ t.FileName);
}


解决方案

  VAR的查询= DBContext.Table1.Where(C => c.FacilityID == facilityID&放大器;&安培; c.FilePath = NULL&放大器;!&安培; c.TimeStationOffHook< oldDate)
.OrderBy( C => c.FilePath)
.Skip(1000)
。取(1000)
。选择(C =>新建{c.FilePath,c.FileName})
.ToList();
的foreach(查询变种T)
{
Console.WriteLine(t.FilePath +\\+ t.FileName);
}

您需要使用的 选择


I use the query below to get all columns(20 more) in Entity Framework Linq. Because of out of memory exception, I only want to get two of them. One is "FileName", the other one is "FilePath". How to modify my code?

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

解决方案

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

You need to use Select.

这篇关于通过LINQ从表中选择两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆