k80sg
k80sg

Reputation: 2473

Nested linq Select

How, I am trying to bind my 2 of my dropdownlist based on a ProductID and it's available size and available color hence if a user selects say grey, only sizes available for grey will appear and vice versa for the size. I am trying the below linq statement below, apparently it throws a "LINQ to Entities does not recognize the method 'System.String ToString()' method", I am not sure if the linq construction is right either. Can anyone please advice? Thanks.

if (!Page.IsPostBack)
{
    using (CommerceEntities db = new CommerceEntities())
    {
        int tempNum; // ----> The ProductId
        if (Int32.TryParse(litTypeSel.Text, out tempNum))
        {
            if ((ddlColor.SelectedValue == "") && (ddlSize.SelectedValue == ""))
            {
                ddlColor.DataSource = (from p in db.ProductTypes
                                       where p.ProductID == tempNum && (p.Size == (from s in db.ProductTypes select s.Size).ToString())
                                       orderby p.Color
                                       select new { p.Color }).Distinct();
                ddlColor.DataTextField = "Color";
                ddlColor.DataBind();
                ddlColor.Items.Insert(0, new ListItem("Select Color", "NA"));

                ddlSize.DataSource = (from p in db.ProductTypes
                                      where p.ProductID == tempNum && (p.Color == (from c in db.ProductTypes select c.Color).ToString())
                                      orderby p.Size descending
                                      select new { p.Size }).Distinct();
                ddlSize.DataTextField = "Size";
                ddlSize.DataBind();
                ddlSize.Items.Insert(0, new ListItem("Select Size", "NA"));
            }
        }
    }
}

Upvotes: 0

Views: 173

Answers (1)

dahlbyk
dahlbyk

Reputation: 77500

What you're trying to do is more often achieved with a join:

ddlColor.DataSource = (from p in db.ProductTypes
                       join pt in db.ProductTypes
                         on p.Size equals s.Size
                       where p.ProductID == tempNum
                       orderby p.Color
                       select new { p.Color }).Distinct();

However, without an understanding your domain it doesn't seem the addition of that join/filter contributes anything to retrieving the distinct list of colors.

Upvotes: 1

Related Questions