带EF的Linq过滤器收集 [英] Linq filter collection with EF

查看:78
本文介绍了带EF的Linq过滤器收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让Entity Framework选择一个对象并同时过滤它的集合。我有一个JobSeries对象,它有一个作业集合,我需要做的是通过ID选择一个作业系统,并通过SendDate过滤所有的作业,但我不敢相信这个简单的查询是多么困难!

I'm trying to get Entity Framework to select an object and filter its collection at the same time. I have a JobSeries object which has a collection of jobs, what I need to do is select a jobseries by ID and filter all the jobs by SendDate but I can't believe how difficult this simple query is!

这是基本的查询工作:

 var q = from c in KnowledgeStoreEntities.JobSeries
                    .Include("Jobs.Company")
                    .Include("Jobs.Status")
                    .Include("Category")
                    .Include("Category1")
                where c.Id == jobSeriesId
                select c;

任何帮助将不胜感激,我一直在尝试在谷歌中找到一些东西,我想要做在这里: http://blogs.msdn.com/bethmassi/archive/2009/07/16/filtering-entity-framework-collections-in-master-detail-forms.aspx

Any help would be appreciated, I've been trying to find something in google and what I want to do is here:http://blogs.msdn.com/bethmassi/archive/2009/07/16/filtering-entity-framework-collections-in-master-detail-forms.aspx

在VB.NET中,我无法将其转换为C#。

It's in VB.NET though and I couldn't convert it to C#.

编辑:我已经尝试过了它不起作用!:

I've tried this now and it doesn't work!:

            var q = from c in KnowledgeStoreEntities.JobSeries
                                      .Include("Jobs")
                                      .Include("Jobs.Company")
                                      .Include("Jobs.Status")
                                      .Include("Category")
                                      .Include("Category1")
                    where (c.Id == jobSeriesId & c.Jobs.Any(J => J.ArtworkId == "13"))
                    select c;

谢谢

Dan

推荐答案

Include 可以引入性能问题。延迟加载是保证引入性能问题。投影是便宜和容易的:

Include can introduce performance problems. Lazy loading is guaranteed to introduce performance problems. Projection is cheap and easy:

var q = from c in KnowledgeStoreEntities.JobSeries
        where c.Id == jobSeriesId            
        select new 
        {
             SeriesName = c.Name,
             Jobs = from j in c.Jobs
                    where j.SendDate == sendDate
                    select new
                    {
                        Name = j.Name
                    }
             CategoryName = c.Category.Name
        };

显然,我猜这个名字。但是请注意:

Obviously, I'm guessing at the names. But note:


  1. 过滤工作

  2. SQL是

  3. 任何地方都没有类型的字符串。

  4. 您始终可以获取所需的数据,而无需在二进制中指定

  5. 没有对您不需要的列进行检索的带宽惩罚。
  6. $ b $( Include b
  7. EF 4中的免费性能提升。

  1. Filtering works.
  2. SQL is much simpler.
  3. No untyped strings anywhere.
  4. You always get the data you need, without having to specify it in two places (Include and elsewhere).
  5. No bandwith penalties for retrieving columns you don't need.
  6. Free performance boost in EF 4.

关键是在LINQ中思考,而不是在SQL或实现整体实体没有什么好的理由,因为你会使用较老的ORM。

The key is to think in LINQ, rather than in SQL or in materializing entire entities for no good reason as you would with older ORMs.

这篇关于带EF的Linq过滤器收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆