更新外键时Linq to SQL错误

时间:2012-02-02 08:25:54

标签: c# .net linq-to-sql

我是初学者,尝试更新外键但显示此错误

  

无法将类型“DynaDNS.DAO.GroupDetail”隐式转换为   'long'

HostDNSDetail GroupId-FK for Groupdetails HostDNSId

GroupDetails GroupId - PK

public bool UpdateHostDns(HostDNSDetail HostObj)
{ 
DynaDBDataContext ctx = GetDataContext();

var ExistingDns = (from HostDNS in ctx.HostDNSDetails
             where (HostDNS.HostDNSId) == (HostObj.HostDNSId)
             select HostDNS).FirstOrDefault();

   if (ExistingDns != null)
    {
      ExistingDns.GroupId = ctx.GroupDetails.Single(Existing =>Existing.GroupId ==    HostObj.GroupId); 

    // i m trying to update from the value from "HostObj.GroupId".
  }
}

1 个答案:

答案 0 :(得分:3)

您正尝试使用GroupDetails.Single选择整个对象。所以,而不是:

ExistingDns.GroupId = ctx.GroupDetails.Single(Existing =>Existing.GroupId ==    HostObj.GroupId);

你需要做类似的事情:

long ExistingDnsId = (from c in ctx.GroupDetails
                      where c.GroupId == HostObj.GroupId
                      select c.GroupId).SingleOrDefault();