2013-03-18 83 views
2

我们正在使用实体框架不能转换到的System.Guid


private t_Market getMarketByCellsiteID(Guid cellsiteID) 
{ 
    try 
    { 
     t_Market market = null; 
     using (LiveLeaseEntities Entities = new LiveLeaseEntities()) 
     { 
      market = (from m in Entities.t_Market 
         where m.OperatorId = (from o in Entities.t_CellSite 
              where o.CellSiteId == Guid.Parse("53B7B160-20C4-4B60-948A-06570E6E3CBA") 
              select o.OperatorId) 
         select m).Single(); 
      return market; 
     } 
    } 

我得到无法键入 'System.Linq.IQueryable' 隐式转换为 '的System.Guid'

回答

1

我想这应该修复它:

private t_Market getMarketByCellsiteID(Guid cellsiteID) 
{ 
try 
{ 
    t_Market market = null; 
    using (LiveLeaseEntities Entities = new LiveLeaseEntities()) 
    { 
     market = (from m in Entities.t_Market 
        where m.OperatorId == (from o in Entities.t_CellSite 
             where o.CellSiteId == Guid.Parse("53B7B160-20C4-4B60-948A-06570E6E3CBA") 
             select o.OperatorId).Single() 
        select m).Single(); 
     return market; 
    } 
} 

两件事情:你需要一个==做比较m.OperatorId,和你将它与一个返回IQuerable的LINQ查询进行比较 - 在​​LINQ查询中调用Single()执行该查询并返回一个值进行比较。

+0

谢谢你呢 – user2167089 2013-03-18 20:23:06

1

您的问题与此比较:

m.OperatorId = (from o in Entities.t_CellSite 
    where o.CellSiteId == Guid.Parse("53B7B160-20C4-4B60-948A-06570E6E3CBA") 
    select o.OperatorId) 

您正在比较System.GuidSystem.Linq.IQueryable<System.Guid>。如果您希望只有一个结果,你可以这样做:

m.OperatorId = (from o in Entities.t_CellSite 
    where o.CellSiteId == Guid.Parse("53B7B160-20C4-4B60-948A-06570E6E3CBA") 
    select o.OperatorId).First() 
+0

+1不知道为什么OP选择了其他答案 – 2013-03-18 20:52:42

相关问题