2010-01-13 63 views
68

我在基於C#的代碼中使用了實體框架。我遇到了意想不到的奇怪現象,正在尋找建議。實體框架 - 無法將lambda表達式轉換爲鍵入'string',因爲它不是委託類型

案例1,2,3,4 ... 項目:
RivWorks.dll
RivWorks.Service.dll
RivWorks.Alpha.dll

樣品(所有這些工作):
RivWorks.Alpha.dll:

public static bool EndNegotitation(long ProductID) 
{ 
    var product = (from a in _dbFeed.AutoWithImage 
        where a.AutoID == ProductID select a).FirstOrDefault(); 
... 
} 

RivWorks.Service.dll

public static RivWorks.Model.NegotiationAutos.AutoWithImage 
    GetProductById(long productId) 
{ 
    var myProduct = from a in _dbFeed.AutoWithImage 
        where a.AutoID == productId select a; 

    return myProduct.FirstOrDefault(); 
} 
public static List<RivWorks.Model.NegotiationAutos.AutoWithImage> 
    GetProductByCompany(Guid companyId) 
{ 
    var myProduct = from a in _dbFeed.AutoWithImage 
        where a.CompanyID == companyId select a; 

    return myProduct.ToList(); 
} 

案 「怪事」:
RivWorks.Web.Service.dll(WCF項目)
包含的其他項目一樣引用。

public NegotiateSetup GetSetup(string method, string jsonInput) 
{ 
    ... 
    long.TryParse(ProductID, out result); 
    var product = (from a in _dbFeed.AutoWithImage 
        where a.AutoID == result select a).FirstOrDefault(); 
    ... 
} 

我得到這個編譯時錯誤(單詞「其中」在我的編輯被高亮顯示):
無法轉換lambda表達式到類型「串」,因爲它不是一個委託類型

任何想法會導致這種情況?

+0

這聽起來很奇怪。如果您刪除對「FirstOrDefault」的調用,會發生什麼情況?顯然,在你嘗試使用'product'之後會失敗,但是這個語句是否被編譯? – 2010-01-13 16:52:39

+0

另外,如果將其更改爲'var product = _dbFeed.AutoWithImage.Where(a => a.AutoID == result);'然後會發生什麼?讓我們把查詢表達式從混合中刪除... – 2010-01-13 16:53:20

+1

所有這些例子都失敗了。但是,我在所有代碼片段中都使用了Using語句,發現我缺少一個:使用System.Linq;這解決了錯誤。 2010-01-13 17:48:00

回答

106

對於那些對結果感興趣的人:
我在代碼的頭部缺少一個簡單的Using語句。

using System.Linq; 

這固定了它。

+13

我很驚訝,VS和ReSharper都不夠聰明,無法檢測到這種情況。謝天謝地,這比兩者都更聰明。 – bwerks 2011-05-08 06:16:53

+0

謝謝,直到我讀到這些之前,都沒有想過。 – 2012-09-08 01:53:27

+2

我得到了同樣的錯誤信息,我有(和「總是有」)使用System.LINQ。當然,模型成員之一將數據類型從字符串[]更改爲字符串... – 2013-08-07 21:21:12

7

就我而言,我有

Using System.Linq; 

,但我是一個where子句項目之後缺少& &。

糟糕的代碼:

item.Group.ID == grp.ID 
p.Product_Status_Flag == 1 && 
item.Valid 

正確的代碼(沒有錯誤):

item.Group.ID == grp.ID && // <- This was missing and I didn't see it right away. 
p.Product_Status_Flag == 1 && 
item.Valid 

我希望這可以節省某人一段時間。

7

我在一個Razor視圖中的Telerik Grid模板中苦苦掙扎了大約一個小時。在我的情況下,這樣的:

columns.Bound(x => x.ID).Template(@<text><a href="@(Model.AppUrl + AdditionalFeeTypes/Details/" + item.ID)">@item.ID</a></text>); 

應該是這樣的:

columns.Bound(x => x.Id).Template(@<text><a href="@(Model.AppUrl + AdditionalFeeTypes/Details/" + item.Id)">@item.Id</a></text>); 

的情況下的 「ID」 錯了!我希望這可以幫助別人。你可能會因爲把一個不存在的屬性而得到這個錯誤!

0

我有一個類似的問題綁定到Telerik MVC Grid的列。 我有我的ViewModel類的Id屬性。 (受上述克里斯的回答啓發)我將它重命名爲XxxId,問題就消失了。我似乎回想起有關MVC爲Id屬性做些特別的事情。

0

我也有類似的問題看但Rx和我的情況下加

using System; 

幫助。 FWIW

與缺失的擴展方法混淆有時太煩人了。

1

我曾與MVC 3剃鬚刀也許有人有相同的同樣的問題,所以我想演示如何解決它在我的stuation

List<Taksit> lst = db.Taksit.Where(y => y.OgrenciId.Equals(Convert.ToInt32(list[0].Id))).ToList(); 

我試圖用包含但因此OgrenciId爲int我得到的錯誤這裏提到所以通過使用等於問題解決了

2

我偶然發現了這一點,發現了一個不同的修復。我正在使用var query = context.Contacts.Where(c => c.FullNameReverse == "TingTong");並獲得提及的錯誤。錯誤是我使用方法FullNameReverse()作爲屬性FullNameReverse。錯過了()!!!

85

在我來說,這是缺少

using System.Data.Entity;

+0

我有「使用System.Data.EntityClient;」而且,雖然它在那裏,但它變灰/不必要。僅允許System.Data.Entity(不包含「客戶端」)。 – 2013-08-07 21:23:19

+1

米格爾爵士是一位天才。謝謝! – oHoodie 2015-01-30 12:55:18

0

在我來說,我想在clientContext.Load在SharePoint 2013應用程序中使用包含了當這個錯誤。

我已經包含SharePoint客戶端庫,像這樣:

using SP = Microsoft.SharePoint.Client; 

要解決,此外我還添加它沒有命名空間:

using Microsoft.SharePoint.Client; 
using SP = Microsoft.SharePoint.Client; 
0

我的問題涉及到的格式:

.Columns(columns => { 
    columns.Bound(p => p.Id).Filterable(false); 
    columns.Bound(p => p.Name).Width(250); 
    ... 

每p.What有這個錯誤就可以了。

這是一個使用MVC的項目,我發現我的問題是在我的模型中對這些變量(Id,Name等)使用「public static int」或「public static string」。當我刪除所有模型變量的「靜態」時,我不再有錯誤。正在驅使我堅持了大約一天...

0

我有一個稍微不同的版本中的這個問題。
如果您從lambda內部調用(靜態)方法,請檢查其返回類型。如果返回類型應該是IEnumerable(在使用lambda時通常是這樣),但返回對象,顯然你有問題。

7
using System.Linq; 
using System.Data.Entity; 
0

試試 使用System.Linq; 我認爲這會幫助你理清這個問題。

相關問題