【问题标题】:Using id from one table to read value from another table using Linq in sub query在子查询中使用 Linq 从一个表中使用 id 从另一个表中读取值
【发布时间】:2014-03-23 06:49:16
【问题描述】:

您好,我有以下 ling 查询

 var lst=(from p in db.business 
        where p.id == id
        select new pO
        {
            pName = p.name,
            newStructure = p.p_disc.Select(x=>
                                            new newStructure 
                                            {
                                                post_date=x.post_date,  
                                                posted_by_id=x.person.last_name.Where(x.posted_by_id==x.person.p_id)
                                            })
                             });

newStructure 类是

public class newStructureProject
{
        public int posted_by_id { get; set; }
        public System.DateTime post_date { get; set; }
        public person person  { get; set; }

}

在下面的查询行中,我尝试使用 newStructure 中的 posted_by_id 从 person 表中读取姓氏和名字。

posted_by_id=x.person.last_name.Where(x.posted_by_id==x.person.p_id)

我收到 intelisense 错误“字符串不包含 where 的定义..... 请让我知道如何在查询中使用它来使用posted_by_id 来读取person 表中的姓氏和名字,它们具有外键关系。 谢谢

【问题讨论】:

  • 实体是什么,它们之间的关系是什么?
  • 你想在这里做什么? posted_by_id=x.person.last_name.Where(x.posted_by_id==x.person.p_id)
  • 刚刚在帖子中添加了实体
  • 基本上来自 p_disc 的 person_id 使用该 person_id 我正在从人(实体)读取人的姓名
  • 您的查询选择了所需的帖子,不是吗?并且所需的帖子有一个人(导航属性),对吗?导航属性的目的是为您节省额外的外键搜索。我错过了什么吗?

标签: c# mysql sql linq lambda


【解决方案1】:

试试这个姓氏。

posted_by_id=x.person.Where(x=>x.posted_by_id==x.person.p_id).Select(x=>x.last_name).FirstOrDefault()

您需要先在对象上应用 where 子句,然后选择您需要的属性值。

【讨论】:

  • Intelisense 不会让我选择 x.person 的位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
相关资源
最近更新 更多