Entity Framework 4中的查询,方法查询问题
我有一点误会,希望你能为我澄清。 我有一个名为:请求的表。
我想做查询,可以说通过请求 ID,但它不起作用。
public Requests SearchById(int id)
{
Model.ModelContainer cont = new Model.ModelContainer();
return (cont.Requests.Where(req => req.ReqId == id));
//when i try to cast to Request i get an error too
}
我找到了一个示例,该行应该可以工作:
cont.Requests.First(req => req.ReqId == id)
但我在编译过程中收到错误,它不能包含 lambda 表达式。
我有几个问题:
- 我如何处理上面的示例?(从实体查询)
- 除了转换为列表之外,我如何处理多行结果,
- 我需要构建和高级搜索(我有 6-7 列),是否有常见的做法是?
谢谢 !!
i have a little misunderstanding and i hope you can clear that for me.
i have a table called : Requests.
I want to do query, lets say by Request Id but it doesn't work.
public Requests SearchById(int id)
{
Model.ModelContainer cont = new Model.ModelContainer();
return (cont.Requests.Where(req => req.ReqId == id));
//when i try to cast to Request i get an error too
}
i found an example which this line should work :
cont.Requests.First(req => req.ReqId == id)
but i get error during compilation that it cannot contain lambda expression.
i have few question:
- How cant i handle the upper example ?(querying from entity)
- How can i work with multi-line results besides converting to list
- i need to build and advanced search (i have 6-7 columns ), is there a common way to do it?
thanks !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效:
您拥有的是
IEnumerable
,而不是单个请求。还要确保您的代码文件中有一个。当您可以使用
using
块时,也请始终释放上下文。这也应该有效 - 确保您的代码文件中有
using System.Linq
。由于您的上下文是本地的并且在您的方法之外不可用(之后会被处理),因此列表是您的最佳选择。如果您将其保留为 IQueryable ,那么您还没有真正从数据库检索结果,您刚刚定义了查询,这会在消费者检索结果时导致问题,因为只有这样执行数据库查询 - 但相应的数据库上下文可能已被释放。在查询末尾附加
.ToList()
以具体化IQuerable
您可以在 .Where() 扩展方法中组合多个子句,即
Where( x=> xA=="foo" && xB == 42)
。如果需要,您还可以链接多个Where()
方法。This should work:
What you had was an
IEnumerable<Request>
, not a single request. Also make sure you have ain your code file. Also always dispose the context when you can using a
using
block.This should also work - make sure you have a
using System.Linq
in your code file.Since your context is local and not available outside of your method (it gets disposed afterwards) a list is your best option. If you keep it an
IQueryable
then you have not really retrieved the results from the DB yet, you have just defined your query, this leads to a problem when the results are retrieved by the consumer since only then the DB query is executed - but the corresponding DB context might have been disposed already. Append.ToList()
at the end of your query to materialize anIQuerable<T>
You can combine multiple clauses in the .Where() extension method, i.e.
Where( x=> x.A=="foo" && x.B == 42)
. You can also chain multipleWhere()
methods if necessary.您的问题是
Where
返回对象的集合(零到多个)。你只想要一个对象。您不能简单地将对象集合转换为单个对象。相反,您应该获取
First
(或者,如果您知道肯定只有一个可以称为Single
)。关于2 - 这取决于你想做什么。您可以对结果集执行
foreach
操作,或者在将结果集放入列表之前从结果集中进行选择 - 但有时最有效的选择是先放入列表。第三点,有很多可用的模式。同样,这取决于您在做什么以及“搜索词”的工作方式。例如,
Your problem is that
Where
returns a collection of objects (zero to many). You only want a single object. You can't simply cast a collection of objects to a single object.Instead you should get the
First
(or if you know there's definitely going to be only one you can callSingle
).Regarding 2 - that depends what you want to do. You can do
foreach
over the resultset, or select from the resultset before putting it into a list - but sometimes the most efficient option is to put in a list first.And point 3, there are plenty of patterns available. Again it depends on what you're doing and how the 'search terms' work. For example,