(or any other enumerated collection ) What I want , but instead of doing var...">

System.LINQ.Dynamic: select ("new class name (...)") in List <T> (or any other enumerated collection <T>)

What I want , but instead of doing

var carsPartial = cars.Select("new(name, year)").ToList();

I want to do this:

 var carsPartial = cars.Select<car>("new car(name, year)").ToList<car>(); 

Thanks again and ask for clarification if necessary.

car class:

  public class car { public string name { get; set; } public int year { get; set; } } 

What's in cars: a bunch of data about cars with names and years like a database.

+6
source share
2 answers

Do you know the general part of Select at compile time? In your example, is it known, or is it also "dynamic"? If this is known, then you should be able (not tested):

 var carsPartial = cars.Select("new car(name, year)").Cast<car>().ToList(); 
+2
source

Use LINQ and something like:

 var carsPartial = (from c in cars select new car { name = c.name, year = c.year }).ToList(); 

Not fully tested ...

-1
source

Source: https://habr.com/ru/post/921714/


All Articles