C# EntityFramework 加入
C# EntityFramework Join
我想根据数量加入 2 个实体的结果。这就是我所拥有的...
订单 #1
OrderID CustomerID ItemID Description POS# Cost Quantity
1 1 1 Apples 111 1.25 3
1 1 2 Oranges 222 1.12 5
1 1 3 Bananas 333 1.17 5
订单 #2
OrderID CustomerID ItemID Description POS# Cost Quantity
2 1 1 Apples 111 1.25 7
2 1 2 Oranges 222 1.12 2
2 1 3 Bananas 333 1.17 5
这是我用来获取每张票的代码:
public OrderEntity getOrder(int orderId)
{
var data = from c in Orders
where c.OrderID == orderId
select c;
return data;
}
我将如何编写 LINQ 代码来组合 2 张票以便得到数量总和?它应该看起来像这样...
订购门票 #1 和 #2
CustomerID ItemID Description POS# Cost Quantity
1 1 Apples 111 1.25 10
1 2 Oranges 222 1.12 7
1 3 Bananas 333 1.17 10
看来我应该可以做这样的事情...
public List<OrderEntity> getCustomerOrders(int customerId)
{
var data = from c in Orders
where c.CustomerID == customerId
select c;
return data.ToList();
}
问题是我无法弄清楚分组。那里有很多关于如何编写用于分组的 EF 代码的信息,但我不确定我是否应该按 CustomerID 或数量进行分组。非常感谢任何有关如何在此处进行分组的提示。
您应该按 CustomerID
和 ItemID
分组:
尝试这样的事情:
public List<OrderEntity> getCustomerOrders(int customerId)
{
var data = from c in Orders
where c.CustomerID == customerId
group c by new { c.CustomerID, c.ItemID } into g
select new OrderEntity () {
CustomerID = g.Key.CustomerID,
ItemID = g.Key.ItemID,
Quantity = g.Sum(x => x.Quantity)
};
return data.ToList();
}
我不确定您是如何定义数据的,但是如果您需要在结果中包含 Description
POS
、Cost
,请尝试:
public List<OrderEntity> getCustomerOrders(int customerId)
{
var data = from c in Orders
where c.CustomerID == customerId
group c by new { c.CustomerID, c.ItemID,c.Description,c.POST,c.Cost } into g
select new OrderEntity () {
CustomerID = g.Key.CustomerID,
ItemID = g.Key.ItemID,
Quantity = g.Sum(x => x.Quantity),
Description = g.Key.Description,
POST = g.Key.POST,
Cost = g.Key.Cost
};
return data.ToList();
}
我想根据数量加入 2 个实体的结果。这就是我所拥有的...
订单 #1
OrderID CustomerID ItemID Description POS# Cost Quantity
1 1 1 Apples 111 1.25 3
1 1 2 Oranges 222 1.12 5
1 1 3 Bananas 333 1.17 5
订单 #2
OrderID CustomerID ItemID Description POS# Cost Quantity
2 1 1 Apples 111 1.25 7
2 1 2 Oranges 222 1.12 2
2 1 3 Bananas 333 1.17 5
这是我用来获取每张票的代码:
public OrderEntity getOrder(int orderId)
{
var data = from c in Orders
where c.OrderID == orderId
select c;
return data;
}
我将如何编写 LINQ 代码来组合 2 张票以便得到数量总和?它应该看起来像这样...
订购门票 #1 和 #2
CustomerID ItemID Description POS# Cost Quantity
1 1 Apples 111 1.25 10
1 2 Oranges 222 1.12 7
1 3 Bananas 333 1.17 10
看来我应该可以做这样的事情...
public List<OrderEntity> getCustomerOrders(int customerId)
{
var data = from c in Orders
where c.CustomerID == customerId
select c;
return data.ToList();
}
问题是我无法弄清楚分组。那里有很多关于如何编写用于分组的 EF 代码的信息,但我不确定我是否应该按 CustomerID 或数量进行分组。非常感谢任何有关如何在此处进行分组的提示。
您应该按 CustomerID
和 ItemID
分组:
尝试这样的事情:
public List<OrderEntity> getCustomerOrders(int customerId)
{
var data = from c in Orders
where c.CustomerID == customerId
group c by new { c.CustomerID, c.ItemID } into g
select new OrderEntity () {
CustomerID = g.Key.CustomerID,
ItemID = g.Key.ItemID,
Quantity = g.Sum(x => x.Quantity)
};
return data.ToList();
}
我不确定您是如何定义数据的,但是如果您需要在结果中包含 Description
POS
、Cost
,请尝试:
public List<OrderEntity> getCustomerOrders(int customerId)
{
var data = from c in Orders
where c.CustomerID == customerId
group c by new { c.CustomerID, c.ItemID,c.Description,c.POST,c.Cost } into g
select new OrderEntity () {
CustomerID = g.Key.CustomerID,
ItemID = g.Key.ItemID,
Quantity = g.Sum(x => x.Quantity),
Description = g.Key.Description,
POST = g.Key.POST,
Cost = g.Key.Cost
};
return data.ToList();
}