c# - Selecting columns from a LINQ query -
I want to be able to select the columns displayed in my datagrid view. I.e., I have a linq query that gives an IEnumerable, but I do not want to display all the properties of the policy - I want to allow the user to show what to display. So I thought that something like this could work to create a "narrow" object with only columns.
IEnumerable & lt; Policy & gt; Policies = Repository Gate policies (); P = Out of the policies = Negatives = Select new (if user ("propertyon") ()) propertyon = p. Property On, if (userpic ("property two")) PropertyTwo = p.PropertyTwo, ...}; DataGridView1.DataSource = Results;
Is there a great way to do this?
Part 2: Why does this work:
IEnumerable & lt; Policy & gt; Policies = Repository Gate policies (); DataGridView1.DataSource = Policies;
But it is not:
IEnumerable
In the second case nothing is displayed in the grid.
If you need to select the field dynamically, use it in a selection
is an extension method that accepts a string of fields, so that you can decide which fields to select at runtime:
select the public static IQueryable (this IQueryable source , String selector, parameters object [] value);
Example of use:
var products = db.Products. Where (status). Orderbird ("name"). Select ("new ID (ID id, name, model, onsale)");
Part 2: Your grid is expecting a bindable interface such as IList
, but the second code gives the example IQueryable
is. You can call it iliast
to toList ()
.
var can change by result = (Select P from the policies p). List ();
Comments
Post a Comment