Return 非匿名列表 entity framework - c#
Return non-anonymous list entity framework - c#
我在数据库中有 2 tables :
Table:订单 (item_id)
Table: 项目 (item_id)
当我在 entity framework 中进行内部联接时,如下所示,我需要在一个列表中 return 结果来操作它。通常当我在一个 table 中执行 select 时,我 return 来自名称为 table 的实体的 LIST,但我不知道如何 return 当我有 2 个或更多实体时的列表,我的意思是,使用内部连接,我想 return 一个我可以在其他 class 中操作的列表。当我只用于一个实体时,它是完美和简单的。
public List<????????> getTransdataByStatus(string status)
{
contenxt = new Finance_ManagementEntity();
var _result = (from a in contenxt.Orders
join b in contenxt.Items on a.item_id equals b.item_id
select new
{
a.order_numer,
a.total,
b.item_code,
b.item_qty
});
return _result;
}
我不知道如何 return 它!我尝试使用 .TOLIST(),但仍然 "anonymous"。
谢谢
您可以创建一个复合模型,其中 属性 代表每个实体。
public class CompoundModel
{
public Entities.Order { get; set; }
public Entities.Item { get; set; }
}
public List<CompoundModel> getTransdataByStatus(string status)
{
contenxt = new Finance_ManagementEntity();
var _result = (from a in contenxt.Orders
join b in contenxt.Items on a.item_id equals b.item_id
select new CompoundModel
{
Order = a
Item = b
});
return _result;
}
或者,如果您想扁平化结构,可以创建一个只有四个属性的 class。
public class CompoundModel
{
public string OrderNumber { get; set; }
public int Total { get; set; }
public string ItemCode { get; set; }
public int ItemQuantity { get; set }
}
public List<CompoundModel> getTransdataByStatus(string status)
{
contenxt = new Finance_ManagementEntity();
var _result = (from a in contenxt.Orders
join b in contenxt.Items on a.item_id equals b.item_id
select new CompoundModel
{
OrderNumber = a.order_number,
Total = a.total,
ItemCode = b.item_code,
ItemQuantity = b.item_qty
});
return _result;
}
首先你需要创建一个自定义类型,比如
public class OrderItems
{
public int Order_numer { get; set; }
public int Total { get; set; }
public string Item_code { get; set; }
public int Item_qty { get; set; }
}
然后像这样修改你的函数
public List<OrderItems> getTransdataByStatus(string status)
{
contenxt = new Finance_ManagementEntity();
var _result = (from a in contenxt.Orders
join b in contenxt.Items on a.item_id equals b.item_id
select new OrderItems()
{
Order_numer= a.order_numer,
Total= a.total,
Item_code=b.item_code,
Item_qty=b.item_qty
}).ToList();
return _result;
}
希望对你有用。
你的代码的问题是这部分:
select new // This will create an anonymous type
{
a.order_numer,
a.total,
b.item_code,
b.item_qty
}
由于 select 生成匿名类型,您将获得这些匿名类型的列表作为查询的结果。为了获得列表类型的结果,您需要在 select-clause 中指定类型:
select new TypeYouWantToReturn() // This will create an real type
{
PropA = a.order_numer, // You also need to specify the properties
PropB = a.total, // of the class that you want to assign
PropC = b.item_code, // the resulting values of the query.
PropD = b.item_qty
}
现在查询的结果将 return 一个真实类型的列表。您需要最终调用 .ToList() 以便获得列表而不是 select 语句将 return 的 IEnumerable。
我在数据库中有 2 tables :
Table:订单 (item_id) Table: 项目 (item_id)
当我在 entity framework 中进行内部联接时,如下所示,我需要在一个列表中 return 结果来操作它。通常当我在一个 table 中执行 select 时,我 return 来自名称为 table 的实体的 LIST,但我不知道如何 return 当我有 2 个或更多实体时的列表,我的意思是,使用内部连接,我想 return 一个我可以在其他 class 中操作的列表。当我只用于一个实体时,它是完美和简单的。
public List<????????> getTransdataByStatus(string status)
{
contenxt = new Finance_ManagementEntity();
var _result = (from a in contenxt.Orders
join b in contenxt.Items on a.item_id equals b.item_id
select new
{
a.order_numer,
a.total,
b.item_code,
b.item_qty
});
return _result;
}
我不知道如何 return 它!我尝试使用 .TOLIST(),但仍然 "anonymous"。
谢谢
您可以创建一个复合模型,其中 属性 代表每个实体。
public class CompoundModel
{
public Entities.Order { get; set; }
public Entities.Item { get; set; }
}
public List<CompoundModel> getTransdataByStatus(string status)
{
contenxt = new Finance_ManagementEntity();
var _result = (from a in contenxt.Orders
join b in contenxt.Items on a.item_id equals b.item_id
select new CompoundModel
{
Order = a
Item = b
});
return _result;
}
或者,如果您想扁平化结构,可以创建一个只有四个属性的 class。
public class CompoundModel
{
public string OrderNumber { get; set; }
public int Total { get; set; }
public string ItemCode { get; set; }
public int ItemQuantity { get; set }
}
public List<CompoundModel> getTransdataByStatus(string status)
{
contenxt = new Finance_ManagementEntity();
var _result = (from a in contenxt.Orders
join b in contenxt.Items on a.item_id equals b.item_id
select new CompoundModel
{
OrderNumber = a.order_number,
Total = a.total,
ItemCode = b.item_code,
ItemQuantity = b.item_qty
});
return _result;
}
首先你需要创建一个自定义类型,比如
public class OrderItems
{
public int Order_numer { get; set; }
public int Total { get; set; }
public string Item_code { get; set; }
public int Item_qty { get; set; }
}
然后像这样修改你的函数
public List<OrderItems> getTransdataByStatus(string status)
{
contenxt = new Finance_ManagementEntity();
var _result = (from a in contenxt.Orders
join b in contenxt.Items on a.item_id equals b.item_id
select new OrderItems()
{
Order_numer= a.order_numer,
Total= a.total,
Item_code=b.item_code,
Item_qty=b.item_qty
}).ToList();
return _result;
}
希望对你有用。
你的代码的问题是这部分:
select new // This will create an anonymous type
{
a.order_numer,
a.total,
b.item_code,
b.item_qty
}
由于 select 生成匿名类型,您将获得这些匿名类型的列表作为查询的结果。为了获得列表类型的结果,您需要在 select-clause 中指定类型:
select new TypeYouWantToReturn() // This will create an real type
{
PropA = a.order_numer, // You also need to specify the properties
PropB = a.total, // of the class that you want to assign
PropC = b.item_code, // the resulting values of the query.
PropD = b.item_qty
}
现在查询的结果将 return 一个真实类型的列表。您需要最终调用 .ToList() 以便获得列表而不是 select 语句将 return 的 IEnumerable。