asp.net - LINQ and web service cannot return anonymous types, and you cannot construct an object in a query? -
- web services cannot return anonymous type.
- if building linq query using classes through datacontext... cannot construct instances of classes in query.
why want this? want join 3 "tables" or sets of objects. have 3 items foreign key each other. , lowest, detailed of these represented class had fields other 2 represent data those. in linq query want return list of lowest, detailed class. 1 way have decided "join tables together" , return data each of them via linq sql via webservice. may bad practice. not adding additional properties lowest level class.
consider this... (please ignore naming conventions, driven internal consideration) reason need instantiate anonymous type join... don't know why is... if not way error...
from su in _datacontext.gettable<stateupdate>() join sfs in _datacontext.gettable<systemfacetstate>() on new { su.lngsystemfacetstate } equals new { lngsystemfacetstate = sfs.lngsystemfacetstate } join sf in _datacontext.gettable<systemfacet>() on new { sfs.lngsystemfacet } equals new { lngsystemfacet = sf.lngsystemfacet } join s in _datacontext.gettable<system>() on new { sf.lngsystem } equals new {lngsystem = s.lngsystem} select new { lngstateupdate = su.lngstateupdate, strsystemfacet = sf.strsystemfacet, strsystemfacetstate = sfs.strsystemfacetstate, dtmstateupdate = su.dtmstateupdate, dtmendtime = su.dtmendtime, lngduration = su.lngduration, strsystem = s.strsystem } ).tolist();
notice have build anonymous type composed of pieces of each type. have this... (convert known type transport via web service)
result = new list<stateupdate>(from in qr select(new stateupdate { lngstateupdate = a.lngstateupdate, strsystemfacet = a.strsystemfacet, strsystemfacetstate = a.strsystemfacetstate, dtmstateupdate = a.dtmstateupdate, dtmendtime = a.dtmendtime, lngduration = a.lngduration, strsystem = a.strsystem }));
it awful. , perhaps have created awful mess. if way way off track here please guide me light. feel missing fundamental here when adding these "unmapped" properties stateupdate class.
i hope can see doing here can better way it.
you can create 'dto' class contains properties need return , populate instead of anonymous object:
public class result { public string lngstateupdate { get; set; } ... // other properties }
then use this:
from su in _datacontext.gettable<stateupdate>() ... select new result { lngstateupdate = su.lngstateupdate, ... // other properties }
nitpick note - please ditch hungarian notation , camel casing properties :)
Comments
Post a Comment