Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

entity framework - Linq filter collection with EF

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!

This is the basic query which works:

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

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

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

EDIT: 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;

Thanks

Dan

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...