LINQ to Entities 中不可用的实体属性

发布于 2024-12-18 21:44:02 字数 861 浏览 3 评论 0原文

这是我的简化示例问题。

我有两个从 SQL Server 数据库建模的实体:

  1. 订单(可用列 = OrderID、PackageCount、ManufactureDate、ShipDate、StatusID) 主键 = OrderID)
  2. OrderRecipients(可用列 = RecipientID、FirstName、LastName、Address、City、Zip、Country、OrderID) ; 外键 = OrderID)

Orders 和 OrderRecipients 之间存在[一对多]关系。一份订单可以有多个收件人。

我正在尝试通过以下代码提取订单的收件人。

  var allmyrecipients = from o in mycontext.Orders
                        where (o.SiteID.Equals("NYC") || o.SiteID.Equals("SFO"))
                        select o.OrderRecipients;

但是,当我尝试使用以下代码获取收件人的姓名时:

 foreach (var recipient in allmyrecipients)
 {
   Console.WriteLine(recipient.FirstName);
 }

收件人的名字和其他属性在智能感知下拉列表中不可用。我收到“不包含 FirstName 的定义”错误。

这是为什么?有什么补救措施?我在这里做错了什么?我正在使用 VS 2010、Entity Framework 4。

感谢您抽出时间来提供帮助。

Here is my problem with simplified example.

I have two entities modeled from SQL Server database:

  1. Orders (columns available = OrderID, PackageCount,ManufactureDate, ShipDate, StatusID) Primary Key = OrderID)
  2. OrderRecipients (columns available = RecipientID, FirstName, LastName, Address, City, Zip, Country, OrderID; Foreign Key = OrderID)

There is a [1 to many] relationship between Orders and OrderRecipients. One order can have several recipients.

I'm trying to extract the recipients of orders via the following code.

  var allmyrecipients = from o in mycontext.Orders
                        where (o.SiteID.Equals("NYC") || o.SiteID.Equals("SFO"))
                        select o.OrderRecipients;

However when I try to get the names of the recipients with the following code:

 foreach (var recipient in allmyrecipients)
 {
   Console.WriteLine(recipient.FirstName);
 }

the FirstName and other attributes of the recipient are not available in the Intellisense drop-down. I get "does not contain a definition of FirstName" error.

Why is this and what is the remedy? What am I doing wrong here? I'm working with VS 2010, Entity Framework 4.

Thank you for taking time to help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生寂 2024-12-25 21:44:02

编辑:我将其重构为应该使用 SelectMany< 的东西/a>.试一试:

var orders = from o in mycontext.Orders
    where (o.SiteID.Equals("NYC") || o.SiteID.Equals("SFO"))
    select o;

foreach (var recipient in orders.SelectMany(r => r.OrderRecipients)) 
{
    Console.WriteLine(recipient.FirstName); 
}

EDIT: I refactored this into somthing that should work using SelectMany. Give this a shot:

var orders = from o in mycontext.Orders
    where (o.SiteID.Equals("NYC") || o.SiteID.Equals("SFO"))
    select o;

foreach (var recipient in orders.SelectMany(r => r.OrderRecipients)) 
{
    Console.WriteLine(recipient.FirstName); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文