LINQ is used to include nested entities.

entity-framework linq linq-to-entities

Question

Using EF 4.1 code, I'm experimenting with LINQ for the first time.

For instance, I have entities that have nested Lists of other things

class Release
{
    int ReleaseID { get; set; }
    string Title { get; set; }
    ICollection<OriginalTrack> OriginalTracks { get; set; }
}

class OriginalTrack
{
    int OriginalTrackID { get; set; }
    string Title { get; set; }
    ICollection<Release> Releases { get; set; }
    ICollection<OriginalArtist> OriginalArtists { get; set; }
}

class OriginalArtist
{
    int OriginalArtistID { get; set; }
    string Name { get; set; }
    ICollection<OriginalTrack> OriginalTracks { get; set; }
}

What is the easiest method to get all the information for where in one LINQ query?ReleaseID == some value .

Even though I did my research, I came across solutions that implicitly rebuilt an object (often anonymous) with the necessary data. I want the data to come out of the database in exactly the same way as it is stored there, so that if I retrieve a Release object with the appropriate ReleaseID, it will also pull and fill in all the OriginalTrack and OriginalArtist information in the Lists.

Knowing aboutInclude() , however I'm unsure of how to use it for several entities.

We are grateful for any assistance.

1
20
1/12/2014 1:20:24 AM

Accepted Answer

You don't need to use include here,

Simply do the following action

var query = 
    from release in ctx.Releases
    select new {
        release,
        originalTracks = from track in release.OriginalTracks
                         select new {
                               track,
                               releases = track.Releases,
                               orignialArtist = from artist in track.OriginalArtists
                                                select new {
                                                     artist,
                                                     artist.OriginalTracks
                                                }
                         }
        }

var Releases = query.Select(x => x.Release);

You ought to load all of your info.

I used the data from this post here while working.

http://blogs.msdn.com/b/alexj/archive/2009/10/13/tip-37-how-to-do-a-conditional-include.aspx

10
8/24/2011 9:49:04 PM

Popular Answer

Use Include This is the purpose behindInclude there is no need to create several nested select statements.

context.Releases.Include("OriginalTracks.OriginalArtist")
    .Where(release => release.ReleaseID == id);

This keeps your current data structure and is easier to write, read, and understand.

To useInclude The name of the property you wish to retrieve must be specified; it must be the name as it appears in your code, not in a database. For instance:

  • .Include("OriginalTracks") on every Release will have the OriginalTracks attribute
  • .Include("OriginalTracks.OriginalArtist") will include the OriginalArtist on each Track and OriginalTracks property on each Release (Please take notice that containing an OriginalArtist inside including the OriginalTrack is not feasible, either syntactically or logically.)
  • .Include("OriginalTracks").Include("OtherProperty") on every Release will include the OriginalTracks and OtherProperty objects.

As an example, you may link as many of these as you like:

.Include("Tracks.Artist").Include("AnotherProperty")
    .Include("ThirdProperty.SomeItems").Where(r => r.something);

is absolutely true. All that is required is that you place theInclude not on a query, but on the EntitySet - you can't.Where().Include() .



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow