因爲select new { s.width, s.height, s.number}
意味着System.Linq.IQueryable<AnonymousType#1>
但你的函數需要返回IQueryable<Product>
。你的代碼更改爲:
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;
}
更新:
或者,也許你想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;
}
如果你想只有3個屬性width +高度+號創建新的類型。例如:
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;
}