c# - Complex Query LINQ Lambda -
i trying extract data in single query. since involves many tables, sort of stuck @ grouping part.
i not have enough reputation post image of table design. so, giving pk,fk
sector (sectorid) device (deviceid:pk, categoryid:fk) ratio (sectorid,deviceid) use (useid) deviceuse (sectorid,deviceid,useid) family (familyid) category (categoryid) level (levelid) age(ageid) consu (sectorid,deviceid,levelid) distributionone (sectorid,deviceid,levelid) distributiontwo (sectorid,deviceid,levelid, ageid)
what trying achieve is:
given sectorid
, retrieve related information tables given.
the result be:
all devices
grouped family
grouped category
and ratios
(for given sectorid
, deviceid
)
and deviceuses
(for related sectorid
, deviceid
) , related use
deviceid
and consu
(for related deviceid
, levelid
, ageid
) , related age
, level
and distributionone
(for related deviceid
, levelid
, sectorid
) , related level
and distributiontwo
(for related deviceid
, levelid
, sectorid
, ageid
) , related age
, level
so far got method below.
public ienumerable<userconfig> getdevicetype(int sectorid) { var t = repo.getall().asqueryable() .select( c => new userconfig { device = new device { name = c.name, id = c.id }, distributionone = c.distributionone.where(d => d.sectorid == sectorid && d.deviceid == c.id).tolist(), distributiontwo = c.distributiontwo.where(d => d.sectorid == sectorid && d.deviceid == c.id).tolist(), consu = c.consu.where(d=>d.deviceid == c.id).tolist(), category = c.category, family = c.category.family, deviceuse = c.deviceuse.where(d => d.sectorid == sectorid && d.deviceid == c.id).tolist(), ratios = c.ratios.where(d => d.sectorid == sectorid && d.deviceid == c.id).tolist(), use = c.deviceuse.where(d=>d.deviceid==c.id && d.sectorid==sectorid).select(u=>u.use).firstordefault() }); var devices = t.tolist(); return devices; }
where repo
repository of device
getall
repository method set of devices
.
my questions:
am doing right way?
if yes, how group data nested collection of
families
->categories
--->devices
distributionone
distributiontwo
..etc
- if not, need correct (my table design?, query?)
use groupby
operator:
var t = repo.getall().asqueryable() .groupby(c => c.category.family.id) .select(g => new { familyid = g.key, devicesbycategory = g.groupby(c => c.category.id) .select(g2 => new { categoryid = g2.key, devices = g2.select(c => new userconfigs { .... }) }) });
Comments
Post a Comment