Entity Framework 4中的查询,方法查询问题

发布于 2024-11-05 09:10:02 字数 590 浏览 4 评论 0原文

我有一点误会,希望你能为我澄清。 我有一个名为:请求的表。

我想做查询,可以说通过请求 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 表达式。

我有几个问题:

  1. 我如何处理上面的示例?(从实体查询)
  2. 除了转换为列表之外,我如何处理多行结果,
  3. 我需要构建和高级搜索(我有 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:

  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?

thanks !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄风 2024-11-12 09:10:02

这应该有效:

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

您拥有的是 IEnumerable,而不是单个请求。还要确保您

using System.Linq;

的代码文件中有一个。当您可以使用 using 块时,也请始终释放上下文。

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

这也应该有效 - 确保您的代码文件中有 using System.Linq

除了转换为列表之外,我如何处理多行结果

由于您的上下文是本地的并且在您的方法之外不可用(之后会被处理),因此列表是您的最佳选择。如果您将其保留为 IQueryable ,那么您还没有真正从数据库检索结果,您刚刚定义了查询,这会在消费者检索结果时导致问题,因为只有这样执行数据库查询 - 但相应的数据库上下文可能已被释放。在查询末尾附加 .ToList() 以具体化 IQuerable

我需要构建和高级搜索(我有 6-7 列),有吗
常见的做法是?

您可以在 .Where() 扩展方法中组合多个子句,即 Where( x=> xA=="foo" && xB == 42)。如果需要,您还可以链接多个 Where() 方法。

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. 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.

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

This should also work - make sure you have a using System.Linq in your code file.

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. Append .ToList() at the end of your query to materialize an IQuerable<T>

I need to build and advanced search (i have 6-7 columns ), is there a
common way to do it?

You can combine multiple clauses in the .Where() extension method, i.e. Where( x=> x.A=="foo" && x.B == 42). You can also chain multiple Where() methods if necessary.

请止步禁区 2024-11-12 09:10:02

您的问题是 Where 返回对象的集合(零到多个)。你只想要一个对象。您不能简单地将对象集合转换为单个对象。

相反,您应该获取 First (或者,如果您知道肯定只有一个可以称为 Single)。

关于2 - 这取决于你想做什么。您可以对结果集执行 foreach 操作,或者在将结果集放入列表之前从结果集中进行选择 - 但有时最有效的选择是先放入列表。

第三点,有很多可用的模式。同样,这取决于您在做什么以及“搜索词”的工作方式。例如,

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();
}

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 call Single).

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,

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文