c# - How to query the average from a certain property in an Entity and return it in a List of List of objects in LINQ? -
my entities designed in entity framework this:
event
- eventid
- date
- acceleration
- intensity
- deviceid
- blockid
- device
- block
block
- blockid
- datestart
- dateend
- events
device
- deviceid
- alias
- clusterid
- cluster
- events
cluster
- clusterid
- name
- devices
- regionid
region
- regionid
- name
- clusters
an event belongs block , registered device. device belongs cluster , cluster belongs region.
what i'm trying average acceleration , average intensity of events in block, organized in objects in list, these objects must organized depending if want averages of events of block detected devices in every cluster or every region.
what kind of query in linq should use?
to make more clear, these sql string represent actions want perform
select avg(e.intensity) average blocks b, events e, devices d, clusters c, regions r b.blockid = 1 , b.blockid = e.blockid , e.uniqueid= d.uniqueid , d.clusterid = c.clusterid group c.clusterid; select avg(e.intensity) average blocks b, events e, devices d, clusters c, regions r b.blockid = 1 , b.blockid = e.blockid , e.uniqueid= d.uniqueid , d.clusterid = c.clusterid , c.regionid = r.regionid group c.regionid;
maybe (linq entities)...
from ev in db.events ev.block.blockid == 1 group ev ev.device.cluster.region.id g // group ev ev.device.cluster.clusterid g select new { regionid = g.key, // clusterid = g.key, averageintensity = g.average(x => x.intensity), averageacceleration = g.average(x => x.acceleration), };
to add additional fields, e.g. name:
group ev new { id = ev.device.cluster.clusterid, name = ev.device.cluster.name } g
or
group ev ev.device.cluster g // ... clustername = g.key.name,
(sql both same)
you 'group by' many fields - since same (i.e. if id same, name same) - wouldn't change nature of grouping - additional fields attached.
normally (w/o aggregation) can add 'grouping' select group = g,
, flatten , return records etc.
Comments
Post a Comment