我有这些LINQ查询:如何使用对象而不是匿名类型
var type1 = (from ftr in db.TB_FTR
join mst in db.TB_MST on ftr.MST_ID equals mst.MST_ID
join trf in db.TYPE_ID on mst.TYPE_ID equals trf.ID
where ftr.CITY == city && ftr.COUNTY == county
select new MyType { City = ftr.CITY, County = ftr.COUNTY Type = trf.TYPE }
).OrderBy(i => i.City);
var type2 = type1.GroupBy(i => new { i.City, i.County, i.Type })
.Select(group => new { Name = group.Key, MyCount = group.Count() })
.OrderBy(x => x.Name).ThenByDescending(x => x.MyCount)
.GroupBy(g => new { g.Name.City, g.Name.County })
.Select(g => g.Select(g2 =>
new { Name = new { g.Key.City, g.Key.County, g2.Name.Type }, g2.MyCount })).Take(1000).ToList();
正如你所看到的第二个查询返回匿名类型。但我想在一个方法中使用这些查询。所以我不能返回匿名类型。我怎样才能使type2成为非匿名类型?
我已经准备了两个对象:
public class MyType
{
public string City { get; set; }
public string County { get; set; }
public string Type { get; set; }
}
public class ProcessedType
{
public MyType Name {get; set;}
public int MyCount {get; set;}
}
但是因为我可能在查询放错位置的他们,我无法正确地使用它们。你能帮助我,这样我可以使第二个查询返回一个定义的对象?谢谢。