IQueryable C# 选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8936800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IQueryable C# Select
提问by MrZerocaL
this is my code... but i need select only column to display in my Datagridview. I Need the code to select only some columns.. example
这是我的代码...但我只需要选择要在我的 Datagridview 中显示的列。我需要代码来只选择一些列.. 例子
Select{t => t.usu_Login, t => t.usu_Login}
public List<tb_usuario> Get(FilterDefinition filter)
{
var contexto = new indNET_Entities();
IQueryable<tb_usuario> Consulta = contexto.tb_usuario.AsQueryable<tb_usuario>()
.Where(t => t.usu_Ativo == 1)
.OrderBy(t => t.usu_Login);
return Consulta.ToList();
}
采纳答案by Anthony Pegram
If you only want a limited number of columns and you intend to pass the result out of the method, first declare a concrete type to describe the elements.
如果您只需要有限数量的列并打算将结果传递出方法,请首先声明一个具体类型来描述元素。
public class UsuarioData
{
public string UsuLogin { get; set; } // or whatever
public string UsuName { get; set; } // or whatever
}
Then you can use this in the return type for the method
然后你可以在方法的返回类型中使用它
public List<UsuarioData> Get(...)
And finally, use the type in your select.
最后,在您的选择中使用类型。
var consulta = contexto.tb_usuario.Where(whatever).OrderBy(whatever)
.Select(t => new UsuarioData
{
UsuLogin = t.usu_login,
UsuName = t.usu_name
}
);
return consulta.ToList();
And, of course, your callers should expect to get this as the result (or just use type inference with var).
而且,当然,你的调用者应该期望得到这个结果(或者只是使用类型推断var)。
回答by Nickz
Well there is a few ways you could do this, the easiest way:
那么有几种方法可以做到这一点,最简单的方法:
grdvwHoldings.DataSource = Model.Holdings
.Select(x=> new
{ Name= x.HoldingName,
CustomerName = x.FundCustomerName
}).ToList();
grdvwHoldings.DataBind();
Alternatively you could create a class and substitute the new {} for the class and do it at the data layer level.
或者,您可以创建一个类并用新的 {} 替换该类,然后在数据层级别执行此操作。
回答by Sam I am says Reinstate Monica
IQueryable<tb_usuario> Consulta = contexto.tb_usuario.AsQueryable<tb_usuario>()
.Where(t => t.usu_Ativo == 1)
.OrderBy(t => t.usu_Login)
.Select(t => t.ColumnName);
回答by Vinod
Try this:
尝试这个:
(contexto.AsEnumerable()
select new {usu_Login=r.Field<string>("usu_Login")}).ToList();

