IQueryable C# Select

社会主义新天地 提交于 2019-12-03 12:36:41

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

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

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.

Try this:

(contexto.AsEnumerable() 
select new {usu_Login=r.Field<string>("usu_Login")}).ToList();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!