none
Query database using ArrayList elements in the where clause RRS feed

  • Question

  •  

    I am having an issue with this for a while. I have an ArrayList with Guids. I want to use all of these elements to query the database and populate a GridView. This is basically what I have:

     

    ArrayList userCart = (ArrayList)Session["UserCart"];

    WSCDataClassesDataContext dc = new WSCDataClassesDataContext();

    var cartItems = from s in dc.ITEMs

     where s.Item_Cat_No == (Guid)userCart[0]

     select new { s.Item_Image, s.Item_Name, s.Item_Unit_Price, s.Item_Size, s.Item_Material, s.Item_Stock_Qty, s.Item_Color, s.Item_Weight };

     

     

    catViewCartGrid.DataSource = cartItems;

    catViewCartGrid.DataBind();

     

     

     

    Looping will obviously not work,

     

    ArrayList userCart = (ArrayList)Session["UserCart"];

    WSCDataClassesDataContext dc = new WSCDataClassesDataContext();

    foreach(Guid x in userCart)

    {

    var cartItems = from s in dc.ITEMs

     where s.Item_Cat_No == x

     select new { s.Item_Image, s.Item_Name,  s.Item_Unit_Price,s.Item_Size, s.Item_Material, s.Item_Stock_Qty, s.Item_Color, s.Item_Weight };

     

    catViewCartGrid.DataSource = cartItems;

    catViewCartGrid.DataBind();

    }

    Any help is appreciated.

    Monday, October 6, 2008 11:55 PM

Answers

  • Soved it. I used a generic list of Guids instead of the ArrayList

     

     

     

    List<Guid> userCart = (List<Guid>)Session["UserCart"];

    WSCDataClassesDataContext dc = new WSCDataClassesDataContext();

    var cartItems = from s in dc.ITEMs

    // where s.Item_Cat_No == itemNo

    where userCart.Contains(s.Item_Cat_No)

    select new { s.Item_Image, s.Item_Name, s.Item_Unit_Price, s.Item_Size, s.Item_Material, s.Item_Stock_Qty, s.Item_Color, s.Item_Weight };

     

     

    catViewCartGrid.DataSource = cartItems;

    catViewCartGrid.DataBind();

     

     

    The cart now displays all the contents without iterating using a foreach

    Tuesday, October 7, 2008 2:16 AM

All replies

  • Soved it. I used a generic list of Guids instead of the ArrayList

     

     

     

    List<Guid> userCart = (List<Guid>)Session["UserCart"];

    WSCDataClassesDataContext dc = new WSCDataClassesDataContext();

    var cartItems = from s in dc.ITEMs

    // where s.Item_Cat_No == itemNo

    where userCart.Contains(s.Item_Cat_No)

    select new { s.Item_Image, s.Item_Name, s.Item_Unit_Price, s.Item_Size, s.Item_Material, s.Item_Stock_Qty, s.Item_Color, s.Item_Weight };

     

     

    catViewCartGrid.DataSource = cartItems;

    catViewCartGrid.DataBind();

     

     

    The cart now displays all the contents without iterating using a foreach

    Tuesday, October 7, 2008 2:16 AM
  • Thanks a bunch, that was exactly what I was looking to do. Works like a charm.
    Monday, October 27, 2008 8:46 AM