c# - Can not implicitly convert type system.collection.genaric.list to DataModel.resturant.cuisine
问题描述
I find many other questions on the same problem but I can't get any solution
This is my handler function for searching a cuisine in database
public Cuisine GetCuisines(string cuisine)
{
using (ResturantContext context = new ResturantContext())
{
// db.Cuisines.Where(x => x.Name.Contains(Cuisine))
return (from m in context.Cuisines
.Include("Images")
.Include("Category")
where m.Name == cuisine
select m).ToList();
}
}
what is the problem here I can't find out
解决方案
ToList()
creates a List<Cuisines>
but your method returns a single Cuisine
.
You should either change the return type of the method to List<Cuisines>
:
public List<Cuisine> GetCuisines(string cuisine)
...or call FirstOrDefault()
instead of ToList()
to select only a single Cuisines
:
using (ResturantContext context = new ResturantContext())
{
return (from m in context.Cuisines
.Include("Images")
.Include("Category")
where m.Name == cuisine
select m).FirstOrDefault();
}
FirstOrDefault()
will return null
if no matching Cuisines
was found. Single()
will throw an exception.
推荐阅读
- r - 为什么要在 rmarkdown pdf 中自定义输出 pdf 的路径?
- reactjs - ReferenceField 不工作反应管理员
- selenium - 机器人框架-我重命名项目名称和目录名称,之后,在 python 解释器中出现无效错误
- elasticsearch - 如何在 laravel scout 中使用 whereNot
- python-3.x - 用于删除 csv 文件中的重复项的 Python 脚本运行时间过长。我该如何优化它?
- python - 有没有办法从a列中获取值并使用pandas使其成为数据的一种标题?
- angular - Ag-grid es2015 兼容性
- robotframework - 在机器人框架中设置条件 make 选项
- android - 这个可滚动的 android 小部件的名称是什么?
- python-3.x - 在python中清除列表和子列表