logo
down
shadow

"Include" chaining in Entity Framework


"Include" chaining in Entity Framework

By : user2949307
Date : November 16 2020, 06:23 AM
This might help you I must have had a slightly different scenario, had a ToList() somewhere in there, or something similar because I am unable to reproduce the scenario I had a question on:
code :
public class FooBarRepo : IFooBarRepo
{

    private readonly Entities _context;

    public FooBarRepo()
    {
        this._context = new Entities();
    }

    public IQueryable<FooBar> Get()
    {
        return this._context.FooBar;
    }

    public IQueryable<FooBar> GetIncludeFoo()
    {
        return this.Get().Include(i => i.Foo);
    }

    public IQueryable<FooBar> GetIncludeBar()
    {
        return this.Get().Include(i => i.Bar);
    }

    public IQueryable<FooBar> GetIncludeFooAndBar()
    {
        return this
            .Get()
            .Include(i => i.Foo)
            .Include(i => i.Bar);
    }

    public IQueryable<FooBar> GetIncludeFooAndChainBar()
    {
        return this.GetIncludeBar().Include(i => i.Foo);
    }

    public void Dispose()
    {
        this._context.Dispose();
    }
}

class Program
{
    static void Main(string[] args)
    {

        IEnumerable<FooBar> get;
        IEnumerable<FooBar> getIncludeFoo;
        IEnumerable<FooBar> getIncludeBar;
        IEnumerable<FooBar> getIncludeFooAndBar;
        IEnumerable<FooBar> getIncludeFooAndChainBar;

        using (var context = new EntityFrameworkTesting.TestIncludeChaining.Repository.FooBarRepo())
        {
            get = context.Get().ToList();

            getIncludeFoo = context.GetIncludeFoo().ToList();

            getIncludeBar = context.GetIncludeBar().ToList();

            getIncludeFooAndBar = context.GetIncludeFooAndBar().ToList();

            getIncludeFooAndChainBar = context.GetIncludeFooAndChainBar().ToList();
        }
    }
}
-- get
SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[Name] AS [Name]
FROM [dbo].[_FooBar] AS [Extent1]

-- getIncludeFoo
SELECT 
[Project1].[Id] AS [Id], 
[Project1].[Name] AS [Name], 
[Project1].[C1] AS [C1], 
[Project1].[Id1] AS [Id1], 
[Project1].[FooBarId] AS [FooBarId]
FROM ( SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Name] AS [Name], 
    [Extent2].[Id] AS [Id1], 
    [Extent2].[FooBarId] AS [FooBarId], 
    CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
    FROM  [dbo].[_FooBar] AS [Extent1]
    LEFT OUTER JOIN [dbo].[_Foo] AS [Extent2] ON [Extent1].[Id] = [Extent2].[FooBarId]
)  AS [Project1]
ORDER BY [Project1].[Id] ASC, [Project1].[C1] ASC

-- getIncludeBar
SELECT 
[Project1].[Id] AS [Id], 
[Project1].[Name] AS [Name], 
[Project1].[C1] AS [C1], 
[Project1].[Id1] AS [Id1], 
[Project1].[FooBarId] AS [FooBarId]
FROM ( SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Name] AS [Name], 
    [Extent2].[Id] AS [Id1], 
    [Extent2].[FooBarId] AS [FooBarId], 
    CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
    FROM  [dbo].[_FooBar] AS [Extent1]
    LEFT OUTER JOIN [dbo].[_Bar] AS [Extent2] ON [Extent1].[Id] = [Extent2].[FooBarId]
)  AS [Project1]
ORDER BY [Project1].[Id] ASC, [Project1].[C1] ASC

-- getIncludeFooAndBar
SELECT 
[UnionAll1].[Id] AS [C1], 
[UnionAll1].[Id1] AS [C2], 
[UnionAll1].[Name] AS [C3], 
[UnionAll1].[C1] AS [C4], 
[UnionAll1].[Id2] AS [C5], 
[UnionAll1].[FooBarId] AS [C6], 
[UnionAll1].[C2] AS [C7], 
[UnionAll1].[C3] AS [C8]
FROM  (SELECT 
    CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1], 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Id] AS [Id1], 
    [Extent1].[Name] AS [Name], 
    [Extent2].[Id] AS [Id2], 
    [Extent2].[FooBarId] AS [FooBarId], 
    CAST(NULL AS int) AS [C2], 
    CAST(NULL AS int) AS [C3]
    FROM  [dbo].[_FooBar] AS [Extent1]
    LEFT OUTER JOIN [dbo].[_Foo] AS [Extent2] ON [Extent1].[Id] = [Extent2].[FooBarId]
UNION ALL
    SELECT 
    2 AS [C1], 
    [Extent3].[Id] AS [Id], 
    [Extent3].[Id] AS [Id1], 
    [Extent3].[Name] AS [Name], 
    CAST(NULL AS int) AS [C2], 
    CAST(NULL AS int) AS [C3], 
    [Extent4].[Id] AS [Id2], 
    [Extent4].[FooBarId] AS [FooBarId]
    FROM  [dbo].[_FooBar] AS [Extent3]
    INNER JOIN [dbo].[_Bar] AS [Extent4] ON [Extent3].[Id] = [Extent4].[FooBarId]) AS [UnionAll1]
ORDER BY [UnionAll1].[Id1] ASC, [UnionAll1].[C1] ASC

-- getIncludeFooAndChainBar
SELECT 
[UnionAll1].[Id] AS [C1], 
[UnionAll1].[Id1] AS [C2], 
[UnionAll1].[Name] AS [C3], 
[UnionAll1].[C1] AS [C4], 
[UnionAll1].[Id2] AS [C5], 
[UnionAll1].[FooBarId] AS [C6], 
[UnionAll1].[C2] AS [C7], 
[UnionAll1].[C3] AS [C8]
FROM  (SELECT 
    CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1], 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Id] AS [Id1], 
    [Extent1].[Name] AS [Name], 
    [Extent2].[Id] AS [Id2], 
    [Extent2].[FooBarId] AS [FooBarId], 
    CAST(NULL AS int) AS [C2], 
    CAST(NULL AS int) AS [C3]
    FROM  [dbo].[_FooBar] AS [Extent1]
    LEFT OUTER JOIN [dbo].[_Bar] AS [Extent2] ON [Extent1].[Id] = [Extent2].[FooBarId]
UNION ALL
    SELECT 
    2 AS [C1], 
    [Extent3].[Id] AS [Id], 
    [Extent3].[Id] AS [Id1], 
    [Extent3].[Name] AS [Name], 
    CAST(NULL AS int) AS [C2], 
    CAST(NULL AS int) AS [C3], 
    [Extent4].[Id] AS [Id2], 
    [Extent4].[FooBarId] AS [FooBarId]
    FROM  [dbo].[_FooBar] AS [Extent3]
    INNER JOIN [dbo].[_Foo] AS [Extent4] ON [Extent3].[Id] = [Extent4].[FooBarId]) AS [UnionAll1]
ORDER BY [UnionAll1].[Id1] ASC, [UnionAll1].[C1] ASC


Share : facebook icon twitter icon
How to include an inherited property in an "uneven" inheritance with Entity Framework?

How to include an inherited property in an "uneven" inheritance with Entity Framework?


By : Damon William
Date : March 29 2020, 07:55 AM
Any of those help I ended up splitting the query into several parts:
Load the parent item, a "Cart". For each of the different types I got (HotelCartItem and TransferCartItem) I queried the db for a set of only that type:
code :
private IQueryable<T> GetCartItemQuery<T>(Guid CartID) where T : CartItem
{
    if (typeof(T) == typeof(HotelCartItem))
    {
        var q = from ci in db.CartItems.OfType<T>()
                    .Include("Hotel")
                where ci.CART_ID == CartID
                select ci;
        return q;
    }
    else
    {
        var q = from ci in db.CartItems.OfType<T>()
                where ci.CART_ID == CartID
                select ci;
        return q;
    }
}
var hotels = GetCartItemQuery<HotelCartItem>(CartID);
var transfers = GetCartItemQuery<TransferCartItem>(CartID);
How to implement multi layered "Include" in Entity framework 3.5 (VS 2008)

How to implement multi layered "Include" in Entity framework 3.5 (VS 2008)


By : user3180744
Date : March 29 2020, 07:55 AM
it should still fix some issue Include uses .'s to navigate the object graph.
So like .Include("TableA.TableB.TableC")
I can't find "Include" method with lambda expression in Entity framework?

I can't find "Include" method with lambda expression in Entity framework?


By : Tushar Kothawade
Date : March 29 2020, 07:55 AM
wish of those help I am using entity framework and I can't find include method like in this example: , it's not in System.Linq. Add
code :
using System.Data.Entity
Entity Framework Code First Lazy Loading without Include("A") method

Entity Framework Code First Lazy Loading without Include("A") method


By : anw004
Date : March 29 2020, 07:55 AM
it should still fix some issue I have a class:
code :
context.Clients.Include(x => x.A).Include(x => x.B)
How to user "Where" in "Include" and "Select" query in Entity Framework 6

How to user "Where" in "Include" and "Select" query in Entity Framework 6


By : tomjdog
Date : March 29 2020, 07:55 AM
around this issue As you noticed, EF 6 doesn't support to filter in the Include method.
Disclaimer: I'm the owner of the project Entity Framework Plus
code :
return _db.Exams
            .IncludeFilter(e => e.ExamPlaces)
            .IncludeFilter(e => e.ExamPlaces.Select(a => a.Agents
                .Where(agent => agent.AgentId == supervisorId))).ToList();
Related Posts Related Posts :
  • I am trying to do a while loop with a string conditional statement in C#
  • C# 'Cannot access a disposed object. Object name: 'SslStream'.'
  • How to make Gecko use seperate CookieContainer per instance?
  • C# Advanced form "please wait"
  • Send and Receive data C# using network stream
  • How to discover that appsettings changed in C#?
  • Check what needs full trust
  • What is the execution order of an MVC Razor view/layout
  • Table designer (Entity Framework) is too resource intense
  • How to clean up an exception string so it can be displayed via Javascript Alert?
  • Configuring Amazon SES Feedback Notifications via Amazon SNS in ASP.NET MVC (C#)
  • C# api responce and request
  • Dynamodb putitem function not working properly
  • theme in windows phone(light or dark) using c#
  • Backup attached database file(.mdf) using c# and SQL Server
  • What is 'TextFile' and where it is uses in WPF project
  • Using same alias for multiple namespaces
  • how to move mails into separate folders
  • Multithreaded Uploader
  • Memory fragmentation?
  • C# correct exception handling
  • "Could not open macro storage" when accessing using file on another machine
  • How to access other directories of hosted server
  • C# Jagged Array check if value exists/true
  • Why can't I type Clone() properly?
  • exception on accessing dictionary from list
  • Getting the immediate response from server without waiting to 200 message
  • Why am I getting exception Directory Is Not empty?
  • Could not load file or assembly 'CefSharp.dll' or one of its dependencies
  • Sending Email By Using C# in unity3D?
  • Correct usage of await async in webapi
  • Program update code issue
  • Marshal.Copy attempted to read or write protected memory At Random Times
  • Restrict Type variable to specific class or subclass
  • Horizontal text alignment in a PdfPCell
  • C# crashing with Form.show() command, ObjectDisposedException - Deeper look / explanation please
  • Will the result of a LINQ query always be guaranteed to be in the correct order?
  • "Could not find default endpoint element that references contract"
  • Umbraco Request.QueryString is null if it's the first time the page is loaded
  • Error inconsistent accessibility method C#
  • How to program Intel Xeon Phi with C#?
  • remove nested element using regular expression
  • Is there a C# alternative to Java's vararg parameters?
  • Clear particular column values in DataTable
  • how to add event handler to programatically created checkboxes
  • Cannot apply indexing with [] to an expression of type 'System.Collections.Specialized.NameValueCollection'
  • Check for key in pre-existing dictionary in case insensitive manner
  • How to remove year from datetime object?
  • Accessing Settings in different ways
  • "This project is empty" error in Sonarqube
  • How to create reusable icon menu in Xamarin
  • Value Cannot be null in Ado.Net connectivity
  • Adding a custom/dynamic attribute when using XSD.exe
  • How to convert object to correct type
  • Automatically sign out from Forms Authentication in ASP.NET when browser is closed
  • Can a WCF service support both Buffered and Streamed transfer modes?
  • Verify a CA Certificate with a public key in C#
  • How to invoke a Web Service that requires the "patch" verb using the C# WebClient wrapper?
  • Proper way a implementing property based on generic type
  • Closing a form that is created in another thread
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org