ProfK
ProfK

Reputation: 51084

How do dynamically set group by properties and aggregates in a LINQ query?

I assume I need to use the method call syntax instead of the query expression form, and I know the basics of grouping in the latter. Maybe some gurus can give caveats and advice on using group fields and aggregates obtained at runtime from a configuration, for use in a reporting like structure.

Upvotes: 0

Views: 267

Answers (2)

Amy B
Amy B

Reputation: 110161

If your data is in xml, linq to xml would allow you to write queries against it in which the certain inputs are strings.

For example:

System.Xml.Linq.XElement myData = GetData();

System.Xml.Linq.XElement result = new XElement("Result",
  myData.Elements("Customer")
  .GroupBy(e => e.Attributes("Name"))
  .Select(g => new XElement("CustomerResult",
     new XAttribute("Name" = g.Key,
     new XAttribute("Count" = g.Count(),
     new XAttribute("MinDate" = g.Min(e => e.Date)
  )
 );

Upvotes: 0

Christopher Edwards
Christopher Edwards

Reputation: 6659

Have you looked at Dynamic Linq? It should do what you want. Have a look at this post from scottgu's blog.

Upvotes: 1

Related Questions