Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.LINQ.Dynamic: Select(“ new classname (…)”) into a List<T> (or any other enumerable collection of <T>)

Tags:

c#

select

linq

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

car class:

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

What is in cars: a bunch of cars data with names and years sort of like a database.

like image 395
wizage Avatar asked Jul 30 '12 19:07

wizage


1 Answers

Do you know the generic part of the Select at compile time? In your example, is known, or is that also 'dynamic'? If it is known, then you should be able to do (didn't test it):

var carsPartial = cars.Select("new car(name, year)").Cast<car>().ToList();
like image 69
Maarten Avatar answered Oct 03 '22 14:10

Maarten