Accessing Data

Executing a simple query that returns Entities

The example shows how to execute a query that returns a collection of entities. It then iterates through the collection of Users and displays the first and last name of each User. |Insert Image of domain model|

Using LINQ To AgileFx Entities

    Entities entities = new Entities();
    var users = from user in entities.User
                where user.Firstname == "Mark" 
                  && user.Designation == "IT Manager"
                select user;

    foreach(var user in users)
    {
        Console.WriteLine("Firstname {0}, Lastname {1}", user.Firstname, user.Lastname);
    }

Using Query Builder

    Entities entities = new Entities();
    var users = entities.CreateQuery<user>()
        .Where(user => user.Firstname == "Mark" 
            && user.Designation == "IT Manager");

    foreach( var user in users)
    {
        Console.WriteLine("Firstname {0}, Lastname {1}", 
            user.Firstname, user.Lastname);
    }

Executing a query that returns a single Entity

The example shows how to execute a query that returns a single User and then displays the first and last name of the user.

    Entities entities = new Entities();
    var usr = entities.CreateQuery<user>()
        .Single(user => user.Id == 23);
    Console.WriteLine("Firstname {0}, Lastname {1}", usr.Firstname, usr.Lastname);

!!Navigate relationships using Navigational Properties

The example shows gets all users with the last name "Mark" and designation "IT Manager. The navigational property user.Account.Status is used to get the Account Status for the user |Insert Image of domain model|

Using LINQ To AgileFx Entities

    Entities entities = new Entities();
    var users = from user in entities.User
                where user.Firstname == "Mark" 
                    && user.Designation == "IT Manager"
                select new { Firstname = user.Firstname, Account = user.Account };
    foreach( var user in users)
    {
        Console.WriteLine("User lastname {0}, Account Status {1}", 
            user.Firstname, user.Account.Status);
    }

Using Query Builder

    Entities entities = new Entities();
    var users = entities.CreateQuery<user>()
        .Where(user => user.Firstname == "Mark" 
            && user.Designation == "IT Manager")
        .Select(user => new { Firstname = user.Firstname, Account = user.Account });
    foreach(var user in users)
    {
        Console.WriteLine("User lastname {0}, Account Status {1}",
            user.Firstname, user.Account.Status);
    }

Explicity Loading related objects

In this example the user account is loaded explicitly. The "LoadRelated" method is available in the namespace "AgileFx.ORM".

    Entities entities = new Entities();
    var users = entities.CreateQuery<user>().LoadRelated(u => u.Account)
        .Where(user => user.Firstname == "Mark" 
            && user.Designation == "IT Manager");
    foreach( var user in users)
    {
        Console.WriteLine("User lastname {0}, Account Status {1}", 
            user.Firstname, user.Account.Status);
    }

In this example also the user account is loaded explicity using the "Load" method, after checking if the account has already been loaded on the User entity. The "Load" and "IsLoaded" methods are available in the namespace "AgileFx.ORM".

    Entities entities = new Entities();
    var usr = entities.CreateQuery<user>()
        .Single(user => user.Firstname == "Mark" 
            && user.Designation == "IT Manager");
    if (!usr.IsLoaded(user => user.Account))
    {
        usr.Load(entities, user => user.Account);
    }
    Console.WriteLine("User lastname {0}, Account Status {1}",
        usr.Firstname, usr.Account.Status);

Explicitly load objects in related collection

The example shows an entity being explicitly loaded from a related collection of the Entity. The Permission Set of each Project for the tenant is loaded. The tenant URL and the Permission Id of each project is displayed. The "LoadRelatedInCollection" method is available in the namespace "AgileFx.ORM".

    Entities entities = new Entities();
    var tnnt = entities.CreateQuery<tenant>().LoadRelated(tenant => tenant.Projects)
        .LoadRelatedInCollection(tenant => tenant.Projects, project => project.PermissionSet)
        .Single(tenant => tenant.Id == 24);
    Console.WriteLine("Tenant URL {0}", tnnt.Url);
    foreach (var project in tnnt.Projects)
    {
        Console.WriteLine("Project Permission Id {0}", project.PermissionSet.Id);
    }