Вопрос

Here's the problem I'm having in my ASP.NET web-form project.

I have Datalist which shows all Products from Database , Each product have one or more colors . At the moment i write queries like this in order to show all product and default photo of that but not colors related to that product :

var query = from p in db.Products
                        from c in db.Pics
                        where c.ProductId == p.ProductId
                        where c.IsDefault == true
                        select new { p.ProductId, p.ProductType, p.Name, p.Number, p.Package, p.Model, p.Size, p.Material, p.MantoLengh, c.PicAddress};

            DataList1.DataSource = query;
            DataList1.DataBind();

I don't know how to bind related colors to a list-control ( like datalist or other list-Controls) inside of my DataList control, here is a view of my database diagram for more information :

enter image description here

Это было полезно?

Решение

I think this query would work though I apologise in advance if my syntax is slightly out, am more VB than C#...

        var query = from p in db.Products 
                    select new { p.ProductId, 
                        p.ProductType, 
                        p.Name, 
                        p.Number, 
                        p.Package, 
                        p.Model, 
                        p.Size, 
                        p.Material, 
                        p.MantoLengh, 
                        p.Pics.Where(x => x.IsDefault).Select(x => x.PicAddress).ToList(), 
                        p.Colors.Select(x => x.ColorHex).ToList() };

Basically, in the anonymouse type you select you can use lambda to fetch the one-to-many collections using Select().ToList(), and use .Where() to perform filtering.

Hope that helps.

EDIT - In response to your comment:

I chose the hex colour strings because they looked like the best bet for creating the actual colours. To cerate a colour from Hex, see this article. Where on the list do you want to add the colours?

Другие советы

I believe that after you have bound the results to your datalist, you should be able to do something like this in your DataList $<%#query.DataItem("Colors")%>

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top