c# - Returning Concatenated String with LINQ for Dropdown -
this follow on question: format list<t> concatenate fields. answer correct, post-answer wanted return list method re-used. got far, not correct (because know iqueryable not correct, prevents anonymoustype error):
public static iqueryable getcitiesincountrywithstate(string isoalpha2) { const string delimiter = ", "; using (var ctx = new atomicentities()) { var query = (from c in ctx.cities join ctry in ctx.countries on c.countryid equals ctry.countryid ctry.isoalpha2 == isoalpha2 select new { cityid = c.countryid, cityname = c.cityname + delimiter + c.state }); return query; } } i want able return list<string> here (if possible) , on ui:
ddlcity.datasource = getcitiesincountrywithstate(session["businesscountry"].tostring()); ddlcity.datatextfield = "cityname"; ddlcity.datavaluefield = "cityid"; ddlcity.databind(); i've tried sorts, without luck. know i've been close - needed! appreciated :)
here option might into:
on city entity, i'm assuming have portion of partial class generated edm tools, , may have portion of partial class contains code not edm generation.
in non-generated portion of partial class, can add transient property gives cityname , state readonly string (see below). transient means property not persisted database, , generated existing fields. technique used lot in viewmodels provide more ui-friendly properties view.
public partial class city { public string citynameandstate { { return cityname + delimiter + state; } } } you can use datatextfield city object in binding. if go route, don't think you'll need anonymous type in query - can return city object is. benefit of doing way vs. returning custom object query if property defined on class, you'll have it, , work same no matter query use retrieve cities.
hope makes sense... made assumptions on how project setup.
Comments
Post a Comment