logo
down
shadow

Linq query for children class by Entity framework


Linq query for children class by Entity framework

By : user2952548
Date : November 19 2020, 01:01 AM
seems to work fine Use the Include method to get the collection of Customers and then use the SelectMany method to project all the CustomerSelectedCovers within all the customers of the given employee , consider the below sample code that simulates your need. ( the minimal properties are put for demo purposes)
code :
class Program
    {
        static void Main(string[] args)
        {
            Employee CurrentEmployee = new Employee();
            List<CustomerSelectedCover> Covers = CurrentEmployee.Customers.
                                                                Include("CustomerSelectedCovers").
                                                                SelectMany(o => o.CustomerSelectedCovers).ToList();


        }
    }
    class Employee
    {
        public int Id;
        public DbSet<Customer> Customers;
    }

    class Customer
    {
        public int Id;
        public int EmployeeId;
        public DbSet<CustomerSelectedCover> CustomerSelectedCovers;

    }
    class CustomerSelectedCover
    {
        public int Id;
        public int CustomerId;
    }


Share : facebook icon twitter icon
Entity framework linq query Include() multiple children entities

Entity framework linq query Include() multiple children entities


By : Luka Stošić
Date : March 29 2020, 07:55 AM
To fix this issue Use extension methods. Replace NameOfContext with the name of your object context.
code :
public static class Extensions{
   public static IQueryable<Company> CompleteCompanies(this NameOfContext context){
         return context.Companies
             .Include("Employee.Employee_Car")
             .Include("Employee.Employee_Country") ;
     }

     public static Company CompanyById(this NameOfContext context, int companyID){
         return context.Companies
             .Include("Employee.Employee_Car")
             .Include("Employee.Employee_Country")
             .FirstOrDefault(c => c.Id == companyID) ;
      }

}
     Company company = 
          context.CompleteCompanies().FirstOrDefault(c => c.Id == companyID);

     //or if you want even more
     Company company = 
          context.CompanyById(companyID);
Entity Framework LINQ Query using Custom C# Class Method - Once yes, once no - because executing on the client or in SQL

Entity Framework LINQ Query using Custom C# Class Method - Once yes, once no - because executing on the client or in SQL


By : ryan.gao
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I think your better of using a Model Defined Function here.
Define a scalar function in your database which returns a DateTime, pass through whatever you need, map it on your model, then use it in your LINQ query:
code :
from g in oc.Users.Where(u => u.aspnet_User.UserName == fbaUsername).First().Groups
  from d in g.Discussions
  select new
  {
   UnReadPostCount = d.Posts.Where(p => p.CreatedDate > myFunkyModelFunction(fbaUsername, p.Discussion)).Count()
  };
WCF Data Services, Entity Framework, LINQ -> Children within children

WCF Data Services, Entity Framework, LINQ -> Children within children


By : juanmonroynieto
Date : March 29 2020, 07:55 AM
Hope that helps So here's what I've found:
First off, WCF Data Services does not support lazy loading proof. This means that this code will never work:
code :
var workOrder = context.WorkOrders.First();
string siteId = workOrder.Unit.Building.Site.Id.ToString(); // won't work w/ WCF Data Services
var query =
    from wo in context.WorkOrders
    join unit in context.Units on wo.Id equals unit.Id
    join building in context.Buildings on unit.Id equals building.Id
    join site in context.Sites on building.Id equals site.Id
    where wo.Id == workOrderId
    select new 
    {
        SiteId = site.Id,
    };
int workOrderId = 1;
var workOrder = context.WorkOrders.Expand("Unit/Building/Site").Where(w => w.Id == workOrderId).First();
string siteId = workOrder.Unit.Building.Site.Id.ToString(); // shows site id
Entity Framework linq query for multiple children

Entity Framework linq query for multiple children


By : Mayur
Date : March 29 2020, 07:55 AM
wish of those help Start your query from YogaSpaces DbSet. This way you can get the result you are expecting from one query executed in the server side:
code :
//Do this outside of your query, a method call can't be translated to sql
var up= DateTime.Now.Date.AddDays(30);
var down= DateTime.Now.Date.AddDays(-30);

var query= dbContext.YogaSpaces
                    .Where(i => i.YogaProfile.ApplicationUserGuid == userId)
                    .SelectMany(i=>i.YogaSpaceEvents.Where(k => k.EventDateTime.Date > down && k.EventDateTime < up));
More efficient way of loading children of entity objects in linq to entity query

More efficient way of loading children of entity objects in linq to entity query


By : Sharad Gaur
Date : March 29 2020, 07:55 AM
will help you Just add this .Include("Domains").Include("DomainEntries") to your Linq in in context.Security_Access That should get rows from those tables all in one go.
So your "inner" queries would look like:
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org