Reputation: 95
I want to store the result of my LINQ query straight into my model but I can only get it working by first storing it into a variable.
var mdl = (from age in dy.vwFacetPool
group age by new { age.AgeGroup, age.AgeString }
into grp
select new { a = grp.Key.AgeGroup, b = grp.Key.AgeString, c = grp.Count() }).ToList();
but I want to store it into a model rather than a variable at first go.
I have the model
public class AgeGroupCounts
{
public int AgeGroup { get; set; }
public string AgeValue { get; set; }
public int AgeCount { get; set; }
}
and then
public class NewEmail
{
public NewEmail()
{
this.Newz = new News();
//this.Age = new AgeGroupCounts();
this.AgeGroup = new List<AgeGroupCounts>();
}
public News Newz { get; set; }
//public AgeGroupCounts Age { get; set; }
public List<AgeGroupCounts> AgeGroup { get; set; }
}
so then i want to map the 3 output fields from the select to the model fields but am lost at this point
model.AgeGroup = (from age in dy.vwFacetPool
group age by new { age.AgeGroup, age.AgeString }
into grp
select new { grp.Key.AgeGroup, grp.Key.AgeString, grp.Count() }).ToList();
I know i can store it into a var and the the model but that seems like i am doing something the wrong way!
Upvotes: 1
Views: 1424
Reputation: 1
Is this what you are looking for?
var mdl = (from age in dy.vwFacetPool
group age by new { age.AgeGroup, age.AgeString }
into grp
select new AgeGroupCounts {
AgeGroup = grp.Key.AgeGroup,
AgeValue = grp.Key.AgeString,
AgeCount = grp.Count()
}).ToList();
Upvotes: 0
Reputation: 12898
I think your select statement shouldn't be an anonymous type...
select new { grp.Key.AgeGroup, grp.Key.AgeString, grp.Count()...
try and change it to
select new AgeGroupCounts() { ... }
Upvotes: 0
Reputation: 169420
If you select an AgeGroupCounts
instead of an anonymous type you will get back a List<AgeGroupCounts>
from the LINQ query:
model.AgeGroup = (from age in dy.vwFacetPool
group age by new { age.AgeGroup, age.AgeString }
into grp
select new AgeGroupCounts { AgeGroup = grp.Key.AgeGroup, AgeValue = grp.Key.AgeString, AgeCount = grp.Count() }).ToList();
Upvotes: 2