2013-03-01 92 views
0

我想LINQ的聲明使用 SELECT * FROM dbo.vwListDetails翻譯那裏的ProductID = '20D9F725-6667-4F3A-893A-7D30FED550BE'LINQ語句返回重複記錄

我不過寫LINQ的聲明它使用SQL statment它上面返回返回不正確的數據

productid       productname custmerid customername 
20D9F725-6667-4F3A-893A-7D30FED550BE   nike   1  andy 
20D9F725-6667-4F3A-893A-7D30FED550BE   nike   2  randy 

public IEnumerable<vwListDetails > GetAllListDetailConsumer(Guid productid) 
{ 
    ObjectQuery<vwListDetails> cust = db.vwListDetails ; 
    IEnumerable<vwListDetails> query = from d in cust 
             where d.productid == productid 
             select d; 
    return query; 

} 

如果我使用LINQ C#代碼以上使其返回

productid         productname custmerid customer name 
    20D9F725-6667-4F3A-893A-7D30FED550BE   nike   1  andy 
    20D9F725-6667-4F3A-893A-7D30FED550BE   nike   1  andy 
+0

它真的不會。我懷疑你在其他地方有bug。 (另外,我會把你的整個方法體寫成'return db.vwListDetails.Where(d => d.productid == new Guid(productid));' - 或者可能在一個語句中構造'Guid',然後在類似的使用它 – 2013-03-01 06:57:22

+3

爲什麼你使用這個條件:'d.productid ==新Guid(productid)'?這真的很奇怪 – MarcinJuraszek 2013-03-01 06:58:17

+2

你的問題目前是誤導性的,因爲你的例子使用'20'作爲產品ID,而你的C#代碼使用GUID,那是什麼? – 2013-03-01 07:03:08

回答

-1

我懷疑你的情況是錯誤的。您的producid將永遠不匹配GUID。

  1. 你做錯了謂詞。 where d.productid == new Guid(productid)
  2. 正如你在Sql的例子中所說的那樣。你提到productid。但是使用LINQ你使用的是GUID。所以修正謂詞。

    var urList= db.vwListDetails.Select(e=>e.productid==yourCondition);

+0

數據已經截然不同了,LINQ只是不會像所描述的查詢所描述的那樣行事 - 問題在於其他地方 – 2013-03-01 06:59:05

+0

@JonSkeet我也提到過,對於預計值icate是錯誤的。 – 2013-03-01 07:00:26

+0

但是'Distinct()'是一個完整的紅鯡魚,我們不知道謂詞錯誤。在SQL查詢中,「20」的示例很糟糕,但完全有可能是從文本文件中將GUID讀作字符串,並且productid屬性確實是GUID。 – 2013-03-01 07:02:18

0

where條件不正確。我認爲你需要那個:

public IEnumerable<vwListDetails > GetAllListDetailConsumer(string productid) 
{ 
    ObjectQuery<vwListDetails> cust = db.vwListDetails ; 
    IEnumerable<vwListDetails> query = from d in cust 
             where d.productid == productid 
             select d; 
    return query; 
} 
+0

如果它返回結果,我不認爲這是錯的。我懷疑最初的例子只是誤導。 – 2013-03-01 07:02:46

+0

仍然返回重複 – Supermode 2013-03-02 01:04:23