2012-08-16 40 views
1

我想要做的是創建一個自定義類型,自定義屬性將存儲記錄中的Id和Name。 (像「223 - 羅伯特史密斯」)。這是我在做什麼:LINQ不會讓我創建一個自定義屬性(如Id + Name)

return (from c in db.Credores 
     where c.Status == true 
     select new CredorCompleto 
     { 
      Id = c.Id, 
      Nome = c.Nome, 
      NomeCompleto = c.Id + c.Nome, 
      CNPJ = c.CNPJ 
     }).ToList(); 

更新:爲定義 'CredorCompleto'

public class CredorCompleto 
{ 
    public string NomeCompleto { get; set; } 
    public string CNPJ { get; set; } 
    public string Nome { get; set; } 
    public int Id { get; set; } 
} 

這就是我得到:

無法施放鍵入System.Int32System.Object。 LINQ to Entities僅支持投射實體數據模型基元類型。

+1

你可以發佈「CredorCompleto」類的定義嗎? – nemesv 2012-08-16 14:11:30

+0

您是否嘗試先將字符串轉換爲字符串?像',(c.Id + c.Nome).ToString()' – 2012-08-16 14:12:08

+3

我很驚訝,有很多答案假設'Id'必須是一個整數和'Nome'一個字符串(這似乎確實很可能),而且這一定是造成這個問題的原因。爲什麼在我們*不知道'CredorCompleto'的實際定義時回答這個問題,爲了合理地回答這個問題,瞭解這一點非常重要? - 現在我們有6個答案都基本上陳述相同的事情,它*可能*甚至是錯的。 – stakx 2012-08-16 14:15:53

回答

4

你對@月亮的答案提供了一個重要線索評論:「LINQ到實體無法識別方法System.String Format(System.String, System.Object, System.Object)方法,而且這種方法不能被翻譯成店表達」

問題可能db.CredoresIQueryable,而不僅僅是一個IEnumerable。因此,當您的LINQ to SQL提供程序嘗試分析您的原始查詢時,會發現它無法識別,並且不知道如何轉換爲SQL查詢。

我認爲LINQ to SQL提供程序在連接c.Id + c.Nome時轉換爲有效的SQL語句時出現問題,可能是因爲前者是int而後者是string

可以肯定的是,它肯定不知道如何將調用string.Format()轉換爲SQL(這並不奇怪,因爲SQL沒有這個功能)。

因此,您可以嘗試執行SQL查詢,然後在其上執行.NET特定的邏輯。試試這個:

return db 
     .Credores 
     .Where(c => c.Status == true) 
     .AsEnumerable() // <-- this should trigger the execution of the SQL query 
     .ToList()  // <-- and if it does not, then this certainly will 
     .Select(c => new CredorCompleto 
        { 
         … 
        }) 
     .ToList(); 

.AsEnumerable() —的調用和.ToList()呼叫可能需要也,IIRC —將觸發SQL查詢的執行。之後的所有內容都在內存IEnumerable上運行,而不是在IQueryable上運行。這意味着在.ToList()之後,LINQ將不再進行智能代碼分析,或嘗試將其餘運算符轉換爲SQL。

+0

+1是理解實際問題的唯一答案:) – RichK 2012-08-16 14:48:10

+0

就是這樣。謝謝! – 2012-08-16 14:53:55

-2

你問

類似 「223 - 羅伯特·史密斯」)。

使用此

return (from c in db.Credores 
         where c.Status == true 
         select new CredorCompleto 
         { 
          Id = c.Id, 
          Nome = c.Nome, 
          NomeCompleto = c.Id + " - " + c.Nome, 
          CNPJ = c.CNPJ 
         }).ToList(); 

編輯:看到你的階級結構後,我猜你的錯誤是一些地方比這問題沒有給出。

+1

除了以不同的方式格式化'NomeCompleto',這是如何解決類型轉換異常? – stakx 2012-08-16 14:13:56

+0

@stakx:這將返回一個字符串。 – 2012-08-16 14:15:34

+1

你怎麼知道'NomeCompleto'有'string'類型?看起來可能,但你猜測。畢竟,錯誤消息說'int'不能被轉換爲「對象」。 (注意:它不會說'string'!) – stakx 2012-08-16 14:20:53

-2

嘗試

return (from c in db.Credores 
    where c.Status == true 
    select new CredorCompleto 
    { 
     Id = c.Id, 
     Nome = c.Nome, 
     DescricaoCompleta = c.Id+"-"+c.Nome, 
     CNPJ = c.CNPJ 
    }).ToList(); 
-1

你試圖連接int和一個字符串。試試...

NomeCompleto = c.Id.ToString() + " - " + c.Nome, 
+0

「LINQ to Entities不識別方法'System.String格式(System.String,System.Object,System.Object)'方法,並且此方法不能轉換爲存儲表達式。」 – 2012-08-16 14:23:34

0

假設.ToString()c.Idc.Nome回到應有的價值,你可以這樣做:

return (from c in db.Credores 
     where c.Status == true 
     select c).AsEnumerable() 
     .select(x => new CredorCompleto() 
     { 
      Id = c.Id.ToString(), 
      Nome = c.Nome, 
      NomeCompleto = string.Format("{0} - {1}", c.Id, c.Nome), 
      CNPJ = c.CNPJ 
     }).ToList(); 

至於建議的Victor, 「LINQ到實體無法識別方法System.String Format(System.String, System.Object, System.Object)

由於這個原因,使用AsEnumerable()來強制用Linq對對象進行評估。

+0

「LINQ to Entities無法識別方法'System.String Format(System.String,System.Object,System.Object)'方法,並且此方法無法轉換爲存儲表達式。」 – 2012-08-16 14:23:56

+0

+1是第一個答案(IIRC)認識到錯誤的原因(最後一句)。 – stakx 2012-08-16 21:53:16

相關問題