无法隐式转换类型'system.linq.iQueryable,存在显式转换 [英] cannot implicitly convert type 'system.linq.iQueryable, an explicit conversion exist
问题描述
伙计们,我试图在我的Business Object类中编写此查询,当我尝试返回var matchingChild时,我收到一条如上所述的错误消息
hey guys I am trying to write this query in my Business Object class, when I try to return var matchingChild I get an error message as above
public static List<Case_MC_ReporterView> Search(string txt)
{
List<Case_MC_ReporterView> l = new List<Case_MC_ReporterView>();
MissingChildDBDataContext db = new MissingChildDBDataContext();
var matchingChild = from children in db.Case_MC_ReporterViews
where children.MC_FirstName.StartsWith(txt)
select children;
return matchingChild;
}
有人可以帮我吗?
非常感谢,
Chetan.
}
Can anybody help me with this?
many Thanks,
Chetan.
推荐答案
而不是
return matchingChild;
写
write
return matchingChild.ToList();
所有Linq to sql查询都使用Microsoft调用的机制
延迟执行,这意味着直到您对查询进行枚举(遍历结果)后,查询才真正执行.
当您使用
枚举结果时
1)ToList
2)ToArray
3)FirstOrDefault,First
4)SingOrDefault,单个
5)计数
等等等等...
都是IEnumerable接口的所有扩展方法
(哪个IQueryable继承自)
因此,只需返回列表操作即可,而不是返回延迟的查询.
All Linq to sql Queries use a mechanism microsoft calls
Deferred execution, which means the query is not realy executed until you enumerate it (Go Over the results).
When you enumerate the results with
1) ToList
2) ToArray
3) FirstOrDefault, First
4) SingOrDefault, Single
5) Count
Etc, etc...
Which are all extension methods of IEnumerable Interface
(Which IQueryable Inherits from)
So instead of returning the deferred query, just do a to list operation.
这篇关于无法隐式转换类型'system.linq.iQueryable,存在显式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!