Passing anonymous types in ASP.NET MVC

asp.net-mvc, c#, linq-to-sql

Solution

Because `select new { s.width, s.height, s.number}` means `System.Linq.IQueryable<AnonymousType#1>` but your function expects to return `IQueryable<Product>`. Change your code to:

public IQueryable<Product> ListProducts(string prodcutType)
{

    var results = from p in db.Products
                  join s in db.Stocks
                  on p.ID equals s.IDProduct
                  where p.ptype == prodcutType
                  select p;
    return results;
}

UPDATED:

Or maybe you want `IQueryable<Stock>`:

public IQueryable<Stock> ListProducts(string prodcutType)
{

    var results = from p in db.Products
                  join s in db.Stocks
                  on p.ID equals s.IDProduct
                  where p.ptype == prodcutType
                  select s;
    return results;
}

If you want only 3 properties width+height+number create new type. For example:

public class SomeType {
    public int Width { get; set; }
    public int Height { get; set; }
    public int Number { get; set; }
}

public IQueryable<SomeType> ListProducts(string prodcutType)
{

    var results = from p in db.Products
                  join s in db.Stocks
                  on p.ID equals s.IDProduct
                  where p.ptype == prodcutType
                  select new SomeType {
                      Width = s.width,
                      Height = s.height,
                      Number = s.number
                  };
    return results;
}

Problem

I am using ASP.net MVC with C#. Why this is code: ``` public IQueryable<Product> ListProducts(string prodcutType) { var results = from p in db.Products join s in db.Stocks on p.ID equals s.IDProduct where p.ptype == prodcutType select new { s.width, s.height, s.number}; return results; } ``` showing the following error? Error 1 Cannot implicitly convert type `System.Linq.IQueryable<AnonymousType#1>` to `System.Linq.IQueryable<eim.Models.Product>`. An explicit conversion exists (are you missing a cast?)

Original source