Vic*_*orC 2 c# asp.net-mvc linq-to-sql
我在C#中使用ASP.net MVC.为什么这是代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
显示以下错误?
错误1无法将类型隐式转换
System.Linq.IQueryable<AnonymousType#1>为System.Linq.IQueryable<eim.Models.Product>.存在显式转换(您是否错过了演员?)
因为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;
}
Run Code Online (Sandbox Code Playgroud)
更新:
或许你想要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;
}
Run Code Online (Sandbox Code Playgroud)
如果只想要3个属性width + height + number,则创建新类型.例如:
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2055 次 |
| 最近记录: |