I have the following class which returns an expression that then returns data. It seems a tad over complicated and I'm pretty sure LINQ has something to offer that makes my life much easier. But I simply cannot get it.
/// <summary>Provides methods and properties to query location details from a database.</summary>
internal sealed class LocationDatabaseDetailsRequest : DatabaseDetailsRequest<LocationContext, LocationDataContract, Guid>
{
/// <inheritdoc />
public override Func<LocationContext, LocationDataContract> Resource
{
get
{
return
c =>
c.Locations.Select(
l =>
new LocationDataContract
{
Id = l.Id,
Coordinates = l.Coordinates,
Description = l.Description,
Name = l.Name,
Type = l.Type,
Details = this.GetDetails(l.Type, c)
}).SingleOrDefault(i => i.Id == this.Identifier);
}
}
private LocationDetailsDataContract GetDetails(string detailsType, LocationContext locationContext)
{
switch (detailsType)
{
case "City":
return locationContext.Set<CityDetailsDataContract>().SingleOrDefault(i => i.Id == this.Identifier);
case "Planet":
return locationContext.Set<PlanetDetailsDataContract>().SingleOrDefault(i => i.Id == this.Identifier);
case "StarSystem":
return locationContext.Set<StarSystemDetailsDataContract>().SingleOrDefault(i => i.Id == this.Identifier);
case "Sector":
return locationContext.Set<SectorDetailsDataContract>().SingleOrDefault(i => i.Id == this.Identifier);
default:
return null;
}
}
}
My problem is the method call. I know which sets the data context has, but they are fluid, meaning it is certain that new sets will get added.
Can I write this query shorter and well... better?
Resource
fit in? Maybe remove that and add enough code so that everything compiles. – Pierre Menard Mar 18 '15 at 0:59.Set<T>
method is a method from entity framework. I generates a DbSet of typeT
, which contains the database data. – Ruhrpottpatriot Mar 30 '15 at 19:01