-----------------Sqlserver表数据Category--------------------------
表结构:
ID Category
1 分类1
2 分类2
3 分类3
4 分类4
5 分类5 -----------------Sqlserver表数据Detail--------------------------
表结构:
ID CategoryID Name
1 1 子类1
2 1 子类23 2 子类3
4 2 子类3
5 2 子类3--------------需用Linq查出结果---------------------------------- [{"CategoryName":"分类1",
"DetailCount":2,
Items:[{"ItemName":"子类1",Count:1}
{"ItemName":"子类2",Count:1}]
}
{"CategoryName":"分类2",
"DetailCount":3,
Items:[{"ItemName":"子类3",Count:3}]
}
]-------------已建立的实体类-------------------------------------
class Category
//大类名称
public string CategoryName;
//下面子类总数
public int DetailCount;
//子类list
public List<Detail> item;class Detail
//子类名称
public string ItemName;
//子类个数
public int Count;
LINQ类
表结构:
ID Category
1 分类1
2 分类2
3 分类3
4 分类4
5 分类5 -----------------Sqlserver表数据Detail--------------------------
表结构:
ID CategoryID Name
1 1 子类1
2 1 子类23 2 子类3
4 2 子类3
5 2 子类3--------------需用Linq查出结果---------------------------------- [{"CategoryName":"分类1",
"DetailCount":2,
Items:[{"ItemName":"子类1",Count:1}
{"ItemName":"子类2",Count:1}]
}
{"CategoryName":"分类2",
"DetailCount":3,
Items:[{"ItemName":"子类3",Count:3}]
}
]-------------已建立的实体类-------------------------------------
class Category
//大类名称
public string CategoryName;
//下面子类总数
public int DetailCount;
//子类list
public List<Detail> item;class Detail
//子类名称
public string ItemName;
//子类个数
public int Count;
LINQ类
join d into Detail
on c.id equals d.CategoryID into g
select new { CategoryName = c.Category, DetailCount = g.Count(), Items = g };
List<LogCategory> category = GetLogCategory().ToList();
//所有LogDetail集合
List<LogDetail> detail = GetLogDetail().ToList();
var result = from c in category
join d into detail
on c.ID == d. into g
select new {CategoryName = c.Category, DetailCount = g.Count(), Items = g };这样好像有错误 大侠
{
static void Main(string[] args)
{
List<Category> categorys = new List<Category>
{
new Category {Id=1,Name="分类1"},
new Category {Id=2,Name="分类2"},
new Category {Id=3,Name="分类3"},
new Category {Id=4,Name="分类4"},
new Category {Id=5,Name="分类5"},
};
List<Detail> details = new List<Detail>
{
new Detail{Id=1,CategoryId=1,Name="子类1-1"},
new Detail{Id=2,CategoryId=1,Name="子类1-2"},
new Detail{Id=3,CategoryId=2,Name="子类2-1"},
new Detail{Id=4,CategoryId=2,Name="子类2-2"},
new Detail{Id=5,CategoryId=3,Name="子类3-1"},
new Detail{Id=6,CategoryId=3,Name="子类3-2"},
new Detail{Id=7,CategoryId=3,Name="子类3-3"},
};
var query = details.GroupBy(g => new { g.CategoryId }).Select(s => new Result
{
CategoryName = categorys.FirstOrDefault(f => f.Id == s.Key.CategoryId).Name,
DetailCount = details.Where(w => w.CategoryId == s.Key.CategoryId).Count(),
Items = details.Where(w => w.CategoryId == s.Key.CategoryId).ToList()
}); }
}
public class Result
{
public string CategoryName { get; set; }
public Int32 DetailCount { get; set; }
public List<Detail> Items { get; set; }
} public class Category
{
public Int32 Id { get; set; }
public string Name { get; set; } }
public class Detail
{
public Int32 Id { get; set; }
public Int32 CategoryId { get; set; }
public string Name { get; set; }
}