0

In Linq2Sql, it was possible to do a query such as:

using (var db = GetDataContent())
            {
                var query = from p in db.Brands
                            where p.Deleted == false
                            select new BrandImageSummary
                            {
                                BrandID = p.BrandID,
                                BrandUrl = p.BrandUrl,
                                Description = p.Description,
                                MetaDescription = p.MetaDescription,
                                MetaKeywords = p.MetaKeywords,
                                MetaTitle = p.MetaTitle,
                                BrandImageUrl = (from p2 in db.SiteImages where p2.FileTypeID == 5 && p2.ForeignID == p.BrandID && p2.Deleted == false orderby p2.Rank select p2.Filename).FirstOrDefault(),
                                Deleted = p.Deleted,
                                SupplierCode = p.SupplierCode,
                                Title = p.Title,
                                Website = p.Website
                            };

                return query.ToList();
            }

With BrandImageUrl being a nested select. Howerver in entity framework, I seem to get the error:

Unable to create a constant value of type 'SiteImage'. Only primitive types or enumeration types are supported in this context.

Is there a way to do this in entity framework?

The idea of the query is to get one brand image, if I was to join, and there was multiple images, I would get multiple rows and I do not want this.

I am using Entity Framework 5.

Thanks for your help

1 Answer 1

1

You should create a one-to-many relation in your model classes.

You can then write

BrandImageUrl = p.BrandImages
                 .Where(i => i.FileTypeID == 5 && !i.Deleted)
                 .OrderBy(i => i.Rank)
                 .Select(i => i.FileName)
                 .FirstOrDefault()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, but is there now way of doing this without setting up reference's? I'm trying to port a application to entity framework and there are lots and lots queries like that..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.