【问题标题】:Linq Find Item by Primary Key and display Name(that lives in a different table)Linq 按主键查找项目并显示名称(位于不同的表中)
【发布时间】:2012-12-10 16:16:52
【问题描述】:

我正在显示我的数据库中的一条记录。该记录从其他表中提取数据并使用主表中的 Int 来表示值,因此 Item 表的 Division 等于 1 并且 Division 表 1 = ALL 。现在我正在显示记录,我试图将 1 变为全部。所有 ID 字段都显示 int。这就是我的代码告诉它要做的事情。但我正在尝试显示名称,当我这样做时,我会得到很多红色。它找不到名称。 CatagoryID 应该是 CategoryName

希望这是有道理的。

if (!IsPostBack)
{
     string v = Request.QueryString["ContactID"];
     int itemid;
     int.TryParse(v, out itemid);
     var customerInfo =  GetCustomerInfo(itemid);
     CONTACTID.Text = customerInfo[0].ContactID.ToString();
     ContactTitle.Text = customerInfo[0].ContactTitlesID.ToString();
     ContactNameB.Text = customerInfo[0].ContactName;
     DropDownAddCategory.Text = customerInfo[0].CategoryID.ToString();
     DDLAddDivision.Text = customerInfo[0].DivisionID.ToString();
     ContactPhoneBox.Text = customerInfo[0].ContactOPhone;
     ContactCellBox.Text = customerInfo[0].ContactCell;
     ContactEmailBox.Text = customerInfo[0].ContactEmail;
     CextB.Text = customerInfo[0].Ext;
}

private List<Solutions.Models.Contact> GetCustomerInfo(int itemid)
{
     using (ItemContext context = new ItemContext())
     {
         return (from c in context.Contacts 
                 where c.ContactID == itemid
                 select c).ToList();
     }
}

这是模型

public class Contact
{
        [ScaffoldColumn(false)]
        public int ContactID { get; set; }    
        public System.DateTime ContactCreated { get; set; }    
        public string ContactName { get; set; }    
        public int? ContactTitlesID { get; set; }    
        public string ContactOPhone { get; set; }    
        public bool cApproved { get; set; }    
        public string User { get; set; }
        public string ContactCell { get; set; }    
        public string ContactEmail { get; set; }    
        public int? DivisionID { get; set; }    
        public int? CategoryID { get; set; }    
        [StringLength(5)]
        public string CExt { get; set; }           
        public virtual Division Division { get; set; }
        public virtual Category Category { get; set; }
        public virtual ContactTitle ContactTitle { get; set; }    
        public string Ext { get; set; }
}

【问题讨论】:

  • 你需要充实一下。 'get a lot of red' 并不能帮助我们回答您的问题。另外,尝试显示名称...在哪里?
  • 它不是从 Other 表中获取数据以提取我正在寻找的 CategoryName。它只显示当前表的值。在这种情况下,context.Contacts 是唯一能够显示的数据。
  • 您的映射是什么样的?我假设您使用的是 linq to sql?
  • @alexjamesbrown 我添加了模型。
  • 你的问题中遗漏了一条关键信息......我已经适当地标记了你的问题

标签: c# asp.net linq entity-framework ef-code-first


【解决方案1】:

使用实体框架,您可以在查询结果中include 相关实体:

 return (from c in context.Contacts.Include("Catagory") 
         where c.ContactID == itemid
         select c).ToList();

这将返回带有类别对象的联系人:customerInfo.Catagory.CategoryName

顺便说一句,不要返回联系人列表并按索引选择第一个(因此可能有索引超出范围异常),而是修改您的方法以返回第一个联系人(或默认值,如果未找到):

private Solutions.Models.Contact GetCustomerInfo(int itemid)
{
     return (from c in context.Contacts.Include("Catagory") 
             where c.ContactID == itemid
             select c).FirstOrDefault();
}

并以这种方式使用它:

var customerInfo =  GetCustomerInfo(itemid);
if (customerInfo != null)
{        
    CONTACTID.Text = customerInfo.ContactID.ToString();
    // etc
}

【讨论】:

    【解决方案2】:

    您使用的是 LINQ to SQL 还是实体框架?再次检查您的模型并确保两个表之间的关系设置正确。模型中可能缺少该关系,并导致此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-14
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多