Select two columns using lambda expression

I have a table with multiple columns (clm1-clm10). Dantard is filled with all columns as follows:

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

GetDBTablesQuery()defined domainservices.csas follows:

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

How can I display only two columns (e.g. clm1 and clm5) using a dedicated lambda expression?

+4
source share
1 answer

Is this what you are looking for?

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

This will result in an anonymous type. If you want this to result in some type that you defined, it could be something like this:

GetDBTables().Select(o => new MyViewModel { clm1 = o.clm1, clm5 = o.clm5 });
+15
source

All Articles