Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select two columns using lambda expression

Tags:

c#

I have a table with several columns (clm1-clm10). The datagrid is populated with all columns as follows:

MyTableDomainContext context = new MyTableDomainContext();
dataGrid1.ItemsSource = context.DBTables;
context.Load(context.GetDBTablesQuery());

GetDBTablesQuery() is defined in domainservices.cs as follows:

public IQueryable<DBTable> GetDBTables()
{
    return this.ObjectContext.DBTables;
}

How can I displayed only two columns (e.g. clm1 and clm5) using the select lambda expression?

like image 531
user2835586 Avatar asked Oct 01 '13 16:10

user2835586


1 Answers

Is this what you are looking for?

GetDBTables().Select(o => new { o.clm1, o.clm5 });

It will result in an anonymous type. If you want it to result in some type you have defined it could something like this:

GetDBTables().Select(o => new MyViewModel { clm1 = o.clm1, clm5 = o.clm5 });
like image 175
Jeremy Cook Avatar answered Sep 22 '22 08:09

Jeremy Cook