6

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.

Community
  • 1
  • 1
wizage
  • 304
  • 4
  • 20
  • I'm not really familiar with passing a string to `Select`, but could you do `car.Select("new car(name, year)").Cast().ToList();`? – cadrell0 Jul 30 '12 at 19:48
  • 2
    remove the quote marks and change to `new car { Name = name, Year = year }` – Tergiver Jul 30 '12 at 19:48
  • What's in the cars collection?? – Steen Tøttrup Jul 30 '12 at 19:49
  • 1
    And why is your "car" class called "Persons"? – Jon Skeet Jul 30 '12 at 19:49
  • um @Tergiver I am trying to make this dynamic so I can later select the fields that are displayed in the xml – wizage Jul 30 '12 at 19:51
  • @cadrell0 only if it was that easy :( I can't cast type DynamicClass1 to car – wizage Jul 30 '12 at 19:54
  • @JonSkeet just messed up and fixed thanks – wizage Jul 30 '12 at 19:55
  • a: what is `carsPartial` (it matters), and b: there **is** no `car(name, year)` constructor; is that meant to be doing to equivalent of `x => new car { name = x.name, year = x.year }` ? or...? But perhaps the most important thing is: what is `carsPartial`. – Marc Gravell Aug 09 '12 at 06:07
  • @MarcGravell someone edit it and said that was proper way to label my variables what I really meant was this : ' var lQuery = cars.db.Select("x => new car { name = x.name, year = x.year }")' then with the lQuery I would like to be able to use my own xmlserializer to convert it to xml. – wizage Aug 09 '12 at 16:19
  • @wizage simple question: if everything is so dynamic, why are you using LINQ and classes? Would a simple ADO.NET query not be much easier? Likewise, your custom XML serializer could then work in this directly... Just saying. You seem to be waNting to add complex middle steps / plumbing for no obvious reason... – Marc Gravell Aug 09 '12 at 17:25
  • If I do ADO.NET then I run into a reader problem because if I say ' string command = string.Format(@"SELECT "+selectcommand+" FROM Cars);'Then do ' var getperson = new SqlCommand(command, connection); SqlDataReader reader = getperson.ExecuteReader();'Then I have to do a 'while (reader.Read())' which then I have to do all of this reader stuff like checking if in my sql select command if I requested it and if I did then it would read for that other wise it would not do it which makes it really long when you have a big database with a lot of feilds this is my reader for all: – wizage Aug 09 '12 at 19:30
  • if (bCompany){CompanyName = reader["Company"].ToString();} Thanks! Edit: I am doing the classes for MVC 4 controller and @MarcGravell forgot tag... – wizage Aug 09 '12 at 19:33

2 Answers2

2

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();
Maarten
  • 22,527
  • 3
  • 47
  • 68
  • So I do know what I am casting to which is going to be car but in car will be changing like 'new car( name )' and then later it will be 'new car ( name, year)' and all of it will be string so I can edit it. – wizage Aug 08 '12 at 16:14
-1

Use LINQ and something like:

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

Not fully tested...

Jonathan
  • 4,916
  • 2
  • 20
  • 37
  • I need to have the select command to be a string unlike what you are doing but what you wrote works but it is not dynamic at all. It is static please read both the question and the info. – wizage Jul 30 '12 at 21:47