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

相關問題