2015-04-03 122 views
0

这是我的代码,用于显示列表中的数据。它显示数据,但也显示重复的产品。所以,请给我建议的方式来展示产品uniquely.For例 如果我加入一个新的产品到购物车它复制产品先前添加的产品如何显示产品列表中存储的唯一清单产品

private List<Cart> PopulateData() 
{ 

    DataTable dt = new DataTable();  
    dt = (DataTable)Session["Test"]; 

    List<Cart> Product = new List<Cart>(); 

    if (Session["key"] == null) 
    { 

     foreach (DataRow row in dt.Rows) 
     { 
      string Quantity = Request.QueryString["Quantity"]; 

      float f_num = float.Parse(row["ProductPrice"].ToString()); 
      Cart cr = new Cart(); 
      Product.Add(new Cart { ProductName = row["ProductName"].ToString(), ProductPrice = f_num, Quantity = Convert.ToInt32(Quantity), Type = row["Type"].ToString() }); 
     } 

    } 
    else if(Session["key"]!=null) 
    { 
     Product = (List<Cart>)Session["key"]; 
     foreach (DataRow row in dt.Rows) 
     { 
      string Quantity = Request.QueryString["Quantity"]; 

      float f_num = float.Parse(row["ProductPrice"].ToString()); 
      Cart cr = new Cart(); 
      Product.Add(new Cart { ProductName = row["ProductName"].ToString(), ProductPrice = f_num, Quantity = Convert.ToInt32(Quantity), Type = row["Type"].ToString() }); 
     } 

    } 
    Session["key"] = Product; 
    return Product; 
} 
+0

找到这个网址http://stackoverflow.com/questions/1199176/how-to-select-distinct-rows-in-a-datatable-and-store-into-an-array – 2015-04-03 10:55:06

+0

@Praveen不确定的问题。你想只在会话中存储唯一的项目? – JunaidKirkire 2015-04-03 10:55:48

+0

嗨Junaid我想在我的GridView中绑定唯一的数据。我的代码只是复制以前添加的数据。 – Praveen 2015-04-03 10:59:11

回答

0
在其他部分

,(在那里你从添加的数据会话),您必须检查该行是否在会话中,然后不要将其添加到基于某个“uniqueid”的product类中。

else { 
    Product = (List<Cart>)Session["key"]; 
    foreach (DataRow row in dt.Rows) 
    { 
     string Quantity = Request.QueryString["Quantity"]; 
     var uniqueid = Convert.ToInt32(row["productId"]); 
     //Linq to find if the product list has the same item 
     bool containsItem = Product.Any(item => item.ProductId == uniqueid); 
     var uniqueid = Convert.ToInt32(row["uniqueId"]); 

     bool containsItem = Product.Any(item => item.UniqueProperty == uniqueid); 
     float f_num = float.Parse(row["ProductPrice"].ToString()); 
     Cart cr = new Cart(); 
     if(containsItem) { 
     Product.Add(new Cart { ProductName = row["ProductName"].ToString(), ProductPrice = f_num, Quantity = Convert.ToInt32(Quantity), Type = row["Type"].ToString() }); 
     } 
    } 
} 

编辑:代码现在改变。检查正确的产品id属性和字段名

+0

它给出错误在下面一行PLZ建议如何解决错误----- bool containsItem = Product.Any(item => item.UniqueProperty == uniqueid); – Praveen 2015-04-03 11:20:35

+0

'UniqueProperty'是您唯一的ID(产品密钥或其他)的名称 uniqueid是您在记录中的一些uniqueid 您必须根据您拥有的唯一ID构建相同的逻辑。不要只是复制粘贴代码 – 2015-04-03 11:21:50

+0

我是新来的c#所以请根据我的代码建议我应该采取什么UniqueProperty – Praveen 2015-04-03 11:48:01

0

在你,你可以使用LINQ查询

简单重复的记录,在该行

Session["key"] = Product; 

分配产品列表会议时您可以不喜欢它

Session["key"] = Product.GroupBy(x => new { x.Quantity, x.ProductName, x.ProductPrice }).Select(x=>x.Key).ToList(); 

或者

Session["key"] = Product.Select(x => new { Quantity = x.Quantity, ProductName=x.ProductName, ProductPrice=x.ProductPrice }).Distinct(); 
+0

我试过但它给出了错误。所以,请建议锄头克服它。 – Praveen 2015-04-04 05:28:43

+0

@ Ruchir - 无法强制转换System.Collections.Generic.List'1 [<> f__AnonymousType0'3 [System.Int32,System.String,System.Single]]类型的对象以键入System.Collections.Generic 。List'1 [Cart]'----这是错误,它显示PLZ帮助 – Praveen 2015-04-04 05:36:52

+0

您需要投射您的对象,如下所示: 'Session [“key”] = Product.GroupBy(x => new {x .ProductId,x.ProductName})。Select(x => new Cart {ProductId = x.Key.ProductId,ProductName = x.Key.ProductName})。ToList();' – 2015-04-06 08:23:37

相关问题