c# - Strong-type new object after LINQ select statement -
this not critical concept me wondering if strong-type new object after linq
select
statement, rather make anonymous type, in c#
. here sample, defunct of course speaks concept:
public class displayaddress { public int addressid; public string shortaddress; } list<displayaddress> shortaddresses = (from la in longaddresses join ca in customeraddresses on la.addressid equals ca.addressid ca.customerid == selectedcustomer select new { new displayaddress() {addressid = la.addressid, shortaddress = la.line1 + " " + la.city + " " + la.state}}).tolist<displayaddress>();
absolutely, can use expression in select
, including 1 creates new object of type defined. need removing outer new
:
select new displayaddress { addressid = la.addressid , shortaddress = la.line1 + " " + la.city + " " + la.state }).tolist();
note anonymous types typed well. in other words, if do
select new { addressid = la.addressid , shortaddress = la.line1 + " " + la.city + " " + la.state }).tolist();
your anonymous type have 2 strongly-typed fields called addressid
, shortaddress
. difference need consume anonymous objects in context create them in order preserve strong typing. in other words, not able pass results of anonymous select
non-generic function without using dynamic
, object
, etc.
Comments
Post a Comment