2013-03-01 97 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