2013-04-12 58 views
0

我試圖使用LINQ to得到數據庫中的記錄,但它保持不返回記錄如何:LINQ查詢

這是很基本的SQL statment 選擇*其中的productid =「12553」

但是以下代碼不會返回任何結果。請指教。 THX你

private static IEnumerable<ProductModel> GetAllProduct(string productId) 
     { 
      using (var dc = new TestEntities()) 
      { 
       var result = (from a in dc.Products 
           where a.productid == productId 
           select new ProductModel 
           { 
            ProductId = a.productid, 
            Name = a.ProductName 


           }); 
       return result.Distinct().ToList(); 
      } 


     } 
+0

你的LINQ是正確的。它必須是你的數據,而不是你所期望的 –

回答

2

你並不需要投影的位置:

using (var dc = new TestEntities()) 
{ 
    var result = from a in dc.Products 
       where a.productid == productId 
       select a; 
    return result.Distinct().ToList(); 
}