我有这些课程:
public class InitialData
{
public IEnumerable<Employee> Employees { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<Orders> Orders { get; set; }
}
public class Orders
{
public int OrderId { get; set; }
public int Number { get; set; }
}
我有一个存储过程,它返回两个像这样的结果集
Id Name
-- ----
1 John
2 Marc
3 Bruce
Id OrderId Number
-- ------- ------
1 1 23423
1 2 56777
2 3 44445
第二个结果集与员工的ID有关系,员工ID 3没有任何订单。
我想获取所有订单,并将它们放在没有Employee Id的Employee类中, 只是OrderId和Number,因为对于每个Employee,我将有一个Orders属性,它将包含OrderId和Number,所以我知道哪个Employee属于
在我的存储库中,我有:
var model = new InitialData();
using (var db = new AuditContext())
{
var cmd = db.Database.Connection.CreateCommand();
cmd.CommandText = "[dbo].[EnployeeDataSelect]";
cmd.CommandType = CommandType.StoredProcedure;
try
{
db.Database.Connection.Open();
var reader = cmd.ExecuteReader();
reader.NextResult();
model.Employees = ((IObjectContextAdapter)db).ObjectContext.Translate<Employees>(reader).ToList();
Now how can I put the orders under the Employees property of InitialData class filter each with the Id?
???
}
catch (Exception e)
{
}
}
我想过滤每个员工的订单,并将它们放在属性中。订单计入我从存储过程中收到的结果。
答案 0 :(得分:0)
除了将员工ID放在您的订单类中之外,我认为您没有其他选择。
class Order
{
public int Id {get; set;}
public int OrderId {get; set;}
public int Number {get; set;
}
...
var oc = (IObjectContextAdapter)db).ObjectContext;
var orders = oc.Translate<Order>(reader).ToLookup(x => x.Id);
foreach(var employee in model.Employees)
employee.Orders = orders[employee.Id].ToList();
如果您绝对不想将员工ID属性添加到Order
课程,则必须创建另一个课程(例如{{1} })具有employee id属性并将其映射回EmployeeOrder
:
Order