c# - Constructor Invocation using Dynamic Linq Select Expression -
i'm using dynamic linq.
i can following:
iqueryable<person>().select("new(id id, firstname + surname name");
this creates iqueryable
(not iqueryable<object>
) containing anonymous type.
but given have following class:
public class listitem { public listitem() { } public listitem(int id, string name) { this.id = id; this.name = name; } public int id { get; set; } public string name { get; set; } }
i select directly type iqueryable<person>
invoking constructor of listitem
, equivalent following:
iqueryable<person>().select(p => new listitem(p.id, p.firstname + p.surname));
i've tried variants such as:
// illegal - new expects form "new()" - exception "'(' expected" iqueryable<person>().select("new listitem(id, firstname + surname)"); // doesn't work, "no property 'listitem' exists on type 'person'" iqueryable<person>().select("listitem(id, firstname + surname)"); // doesn't work because "[" not expected iqueryable<person>().select("[myproject.utils.listitem,myproject]()");
i believe possible documentation available here:
overload resolution methods, constructors, , indexers uses rules similar c#. in informal terms, overload resolution pick best matching method, constructor, or indexer, or report ambiguity error if no single best match can identified.
note constructor invocations not prefixed new.
is possible or have misunderstood documentation?
i not sure if constructors supported in select
method in dynamic linq. maybe documentation saying supported predicates, expressions used in where
method. not sure though.
anyway, might solution you:
var result = context .customers .select("new(id id, firstname + surname name") .asenumerable() .select(x => new listitem(x.id, x.name)) .tolist();
Comments
Post a Comment