简体   繁体   English

查询实体框架4,查询问题的方法

[英]Querying in Entity Framework 4, method querying problem

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. 我想做查询,让我们说请求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
}

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. 但我在编译期间得到错误,它不能包含lambda表达式。

i have few question: 我有几个问题:

  1. How cant i handle the upper example ?(querying from entity) 我怎么处理上面的例子?(从实体查询)
  2. How can i work with multi-line results besides converting to list 除了转换为列表之外,我如何使用多行结果
  3. i need to build and advanced search (i have 6-7 columns ), is there a common way to do it? 我需要构建和高级搜索(我有6-7列),有没有一种常见的方法来做到这一点?

thanks !! 谢谢 !!

This should work: 这应该工作:

public Request SearchById(int id)
        {
           using(Model.ModelContainer cont = new Model.ModelContainer())
           {
                return cont.Requests.Where(req => req.ReqId == id).FirstOrDefault();
           }
        }

What you had was an IEnumerable<Request> , not a single request. 你有一个IEnumerable<Request> ,而不是一个请求。 Also make sure you have a 还要确保你有一个

using System.Linq;

in your code file. 在你的代码文件中。 Also always dispose the context when you can using a using block. 当您可以使用using块时,也始终处理上下文。

cont.Requests.First(req => req.ReqId == id)

This should also work - make sure you have a using System.Linq in your code file. 这也应该有效 - 确保在代码文件中using System.Linq

How can i work with multi-line results besides converting to list 除了转换为列表之外,我如何使用多行结果

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. 如果你保持IQueryable那么你还没有真正从数据库中检索结果,你刚刚定义了你的查询,这会导致消费者检索结果时出现问题,因为只有这样才能执行数据库查询 - 但是相应的DB上下文可能已经被处理掉了。 Append .ToList() at the end of your query to materialize an IQuerable<T> 在查询末尾附加.ToList()以实现IQuerable<T>

I need to build and advanced search (i have 6-7 columns ), is there a common way to do it? 我需要构建和高级搜索(我有6-7列),有没有一种常见的方法来做到这一点?

You can combine multiple clauses in the .Where() extension method, ie Where( x=> xA=="foo" && xB == 42) . 您可以在.Where()扩展方法中组合多个子句,即Where( x=> xA=="foo" && xB == 42) You can also chain multiple Where() methods if necessary. 如有必要,您还可以链接多个Where()方法。

Your problem is that Where returns a collection of objects (zero to many). 你的问题是Where返回一个对象集合(零到多)。 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 call Single ). 相反,你应该得到第First (或者如果你知道肯定会只有一个你可以称为Single )。

Regarding 2 - that depends what you want to do. 关于2 - 这取决于你想做什么。 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. 你可以做foreach在结果集,或者把它变成一个列表之前从结果选择-但有时最有效的选择是把在列表第一位。

And point 3, there are plenty of patterns available. 第3点,有很多可用的模式。 Again it depends on what you're doing and how the 'search terms' work. 这又取决于你正在做什么以及'搜索术语'是如何工作的。 For example, 例如,

public List<Requests> SearchById(int? id, DateTime? date, string name)
{
    var cont = new Model.ModelContainer();
    var query = cont.Requests;

    if (id != null)
        query = query.Where(req => req.ReqId == id.Value);

    if (date != null)
        query = query.Where(req => req.Date == date.Value);

    if (!String.IsNullOrEmpty(name))
        query = query.Where(req => req.Name == name);

    return query.ToList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM