Return Join Linq C#

Asked

Viewed 328 times

3

I’m trying to return a Join to my class and it’s giving me the following error

Error 1 Cannot implicitly convert type System.Collections.Generic.List in 'System.Collections.Generic.List' C: Projects_asp.NET Advancetechniques Models Repository Productrepository.Cs 40 30 Advancetechniques

Follow the code below.

public List<Line> Get()
{
    return context.
        Lines.
        Join(
            context.Products,
            lines => lines.ID,
            products => products.LineID,
            ((products, lines)
                => new { lines, products}
        )).
        OrderByDescending(products => products.products.ID).ToList();
}

Follows my Entity

public partial class Line
    {
        public Line()
        {
            this.Products = new List<Product>();
        }
        [Required]
        public long ID { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Slug { get; set; }
        [Required]
        public string DesktopImage { get; set; }
        [Required]
        public string MobileImage { get; set; }
        [Required]
        public string AltImage { get; set; }
        [Required]
        public int Position { get; set; }
        [Required]
        public bool IsActive { get; set; }

        public virtual List<Product> Products { get; set; }
    }

1 answer

3

Error happens because you are not returning a list of Line. Try this:

public List<Line> Get()
{
    return context.
        Lines.
        Join(
            context.Products,
            lines => lines.ID,
            products => products.LineID,
            ((products, lines)
                => new { lines, products}
        ))
        .Select(s => s.lines)
        .OrderByDescending(p => p.products.ID).ToList();
}

But it makes no sense to use Join. If Voce won’t use one Where to filter the data, you are simply joining values from 2 tables. Since Line Already has a list of products.. I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.