我的表包含EmployeeID,ProductID和ProductName。该表名为EmployeeProduct,我想进行查询(我将绑定到DataGrid),这将得到如下结果:
Employee ID| ProductID | Name | Count(ProductID)
1 | 2 | XYZ | 3
1 | 5 | ZXY | 2
2 | 2 | XYZ | 1
我试图通过此查询得到这样的信息,但它不会产生结果...... //更新 - 现在我尝试这样做,但仍然没有结果...... //
(Home.xaml.cs)
public class ProductCount
{
public int ProductNumber { get; set; }
public int EmployeeNumber { get; set; }
public int CountProducts { get; set; }
}
public IQueryable<ProductCount> CountProducts()
{
var data = from b in _employ.EmployeeProduct
group b by new { b.EmployeeID, b.ProductID } int z
select new ProductCount { EmployeeNumber = z.Key.EmployeeID, ProductNumber = z.Key.ProductNumber, CountProducts = z.Count()};
return data.AsQueryable();
}
以后在代码中我想将它绑定到我的数据网格,但不幸的是它不会导致错误但是如果我这样做:
dg.ItemsSource = CountProducts();
它没有显示任何东西......
答案 0 :(得分:3)
以下是几种方法:
var employeeProducts = new List<EmployeeProduct>();
employeeProducts.Add(new EmployeeProduct(1, 2, "XYZ"));
employeeProducts.Add(new EmployeeProduct(1, 5, "ZXY"));
employeeProducts.Add(new EmployeeProduct(2, 2, "XYZ"));
var way1 = employeeProducts.Select(
ep => new ProductCount
{
ProductNumber = ep.ProductID,
EmployeeNumber = ep.EmployeeID,
CountProducts = employeeProducts.Count(epc => epc.ProductID == ep.ProductID)
});
var way2 = employeeProducts
.GroupBy(ep => ep.ProductID)
.SelectMany(epg => epg.Select(
ep => new ProductCount
{
ProductNumber = ep.ProductID,
EmployeeNumber = ep.EmployeeID,
CountProducts = epg.Count()
}));
答案 1 :(得分:0)
我认为查询看起来像这样
EntityQuery<EmployeeProduct> query = from c in _employ.CountProduct()
group c by new {c.UserId, c.ProductId, c.Name}
into g
select new {
g.UserId,
g.ProductId,
g.Name,
Count = g.Count()
}