I'm trying to populate a Kendo dropdown using a Read function in a controller, but do so in a way that allows me to get items from one table or a view, based on a column in the view.
Ex.
public class AlternateCountry // <-- Table
{
public Guid CountryGUID { get; set; }
public string Name { get; set; }
}
public class Countries // <-- View
{
public bool hasAltName { get; set; }
public Guid GUID { get; set; }
public string Name { get; set; }
}
I want to show the "Countries" Name column value unless we have an alternate name for that country. Then it would show the Name column in the "AlternateCountry" table. Something like:
var getCountries = (from c in db.Countries
join alt in db.AlternateCountry on c.GUID equals alt.CountryGUID
where c.hasAltName == true
select new {
GUID = c.GUID,
Name = alt.Name
}).ToList();
return Json(getCountries, JsonRequestBehavior.AllowGet);
Problem is this doesn't account for when the alternate name is false, to grab "Name" from the Countries view. I can duplicate another of these blocks and change c.hasAltName == false
, but how do I then combine both of these into one DataSourceResult set?
Actually, I was able to solve this with a union of two queries -- one for the ones without alternate names, one for those with alternate names:
var realCountries = (from c in db.Countries
where c.hasAltName == false
select new {
GUID = c.GUID,
Name = c.Name
}).ToList();
var fakeCountries = (from c in db.Countries
join alt in db.AlternateCountry on c.GUID equals alt.CountryGUID
where c.hasAltName == true
select new {
GUID = alt.GUID,
Name = alt.Name
}).ToList();
var allCountries = realCountries.Union(fakeCountries);
return Json(allCountries, JsonRequestBehavior.AllowGet);