How to do Sub Selects - linq

given the code ( this is a made up example). Also imagine Contract and Plan have a bunch more properties on them.
class Contract
{
public virtual int Id {get; set}
public virtual IList<Plans> {get; set;}
}
class Plan
{
public virtual int Id {get; set; }
}
var blah = query
.SelectMany(contract =>
contract.Plans.Select(plan => new {ContractId = contract.Id, PlanId = plan.Id})
.ToList();
NHibernate will spit the dummy with
Cannot create a LambdaExpression that retrieves the value of '[plan]'
from items with a structure of 'new <>f__AnonymousType168`2(ContractId =
[contract].Id, PlanId = [plan].Id)'. The item expression does not contain the
value or it is too complex.
If I retrieve a list from the database first with ToList() I can easily do this in Linq to Objects and it gives me what I'm expecting. Is there a way to do this with Linq and Nhibernate ?
I mainly want to ensure I'm retrieving the minimal necessary from the DB, in my testing by prefetching from the DB then Linq to objects takes ~ a second, the equivalent SQL takes no measurable time

It should be simple. Try this:
(from contract in query.Contracts
from plan in contract.Plans
select new
{
ContractId = contract.Id,
PlanId = plan.Id
})
.ToList();

Related

Entity Framework 6 Add nested child object in Parent object in LINQ Query

I am working on .NET Core application; Entity Framework 6
I need to add child object which is collection is same parent object in LINQ query not lambda expression
User - parent entity
public class UserDataModel
{
public UserDataModel()
{
this.Roles = new HashSet<UserRoleDataModel>();
}
public Guid Id { get; set; }
public Guid IdentityProviderID {get; set;}
public ICollection<UserRoleDataModel> Roles { get; set; }
}
Child-entity
public class UserRoleDataModel
{
public Guid UserId { get; set; }
public UserDataModel Users { get; set; }
public Guid RoleId { get; set; }
public RoleDataModel Roles { get; set; }
}
LINQ
var uu = (from _user in db.Users
join _userRole in db.UserRoles on _user.Id equals _userRole.UserId
where _user.IdentityProviderID == AureID
select new {
_user,
_userRole
}
This is far I reached but Roles are collection where sub-query to select role separate are IQueryable hence throw conversion/ casting error
var uu = (from _user in db.Users
where _user.IdentityProviderID == AureID
select new UserDataModel
{
Id = _user.Id,
IdentityProviderID = _user.IdentityProviderID,
Roles = (from b in db.UserRoles where b.UserId == userId select b)
}
).ToList();
Addition after comment at the end
It seems you have a true one-to-many relationship between UserData and UserRoles: Every UserData has zero or more UserRoles, and every UserRole belongs to exactly one UserData.
Your class definitions deviate from the Entity Framework Code First conventions. If you'd had your classes configured like a standard Entity Framework One-to-many relationship, entity framework would recognize that you'd meant to design a one-to-many and know automatically when a join is needed in your queries. Reconsider rewriting your classes:
class UserDataModel
{
public GUID Id {get; set;} // primary key
// every UserDataModel has zero or more UserRoleDataModels:
public virtual ICollection<UserRoleDataModel> UserRoleDataModels {get; set;}
... // other properties
}
class UserRoleDataModel
{
public GUID Id {get; set;} // primary key
// every UserDataRoleModel belongs to exactly one UserDataModel
// using foreign key:
public Guid UserDataModelId {get; set;}
public virtual UserDataModel UserDataModel {get; set;}
... // other properties
}
The most important change is that I made the references between your UserDataModel and your UserDataRoleModels virtual. I changed the name of the foreign key, so Entity Framework knows without any attribut / fluent API that you meant to configure a one-to-many.
Back to your question
Given the Id of a UserDataModel, give me the UserDataModel with all
its UserDataRoleModels
After you've rewritten your classes with the proper virtual ICollection, your query would be easy:
var result = myDbContext.Users
.Where(user => user.Id == AureId)
.Select( user => new
{
User = user,
Roles = user.Roles.ToList(),
});
This requirement has the shortcoming that you tend to retrieve to much data. One of the slower parts when querying a database is the transfer of the queried data to your local process. Therefore it is wise not to fetch any more data than you want.
You plan to query a complete UserData with its complete UserRole objects. if the UserData that you fetch has Id XYZ, and it has 50 UserRole objects, then every one of its UserRole object would have a UserDataId with a value of XYZ, thus transferring it 50 times more than needed.
Therefore I advise you to transfer only the data that you actually plan to use:
var result = myDbContext.Users
.Where(user => user.Id == AureId)
.Select( user => new
{
// from the user take only the data you plan to use
Id = user.Id, // probably not, you know that it is AureId
Name = user.Name,
... // other user properties you plan to use
UserRoles = user.UserRoleDataModels.Select(roleData => new
{
// again: only the role data you plan to use
Name = roleData.Name,
Type = roleData.Type,
... // other properties
// certainly not this one:
UserDataId = roleData.UserDataId,
})
.ToList(),
});
This way your query is much faster, you'll only fetch the data you want, you can change the names of fields depending on your needs, and you can add new values if desired, composed from values within UserData
Because of your virtual ICollection you don't need to perform the join. Entity Framework knows your one-to-many and understands that your use of the ICollection should result in an inner join followed by a Group By
Summarized: if you think you'll need a join in entity framework, think twice, you'll probably be able to do it simpler by using the ICollection or the reference to the parent
Addition after comment
if we add role has definition table, how to I add further nested object .
I'm not sure what this means. I think you mean: what to do if a role has a definition table, or what to do if a role has zero or more definition tables.
class UserRoleDataModel
{
public GUID Id {get; set;} // primary key
// every UserDataRoleModel belongs to exactly one UserDataModel
// using foreign key:
public Guid UserDataModelId {get; set;}
public virtual UserDataModel UserDataModel {get; set;}
// every role has exactly one DefinitionTable:
public DefinitionTable DefinitionTable {get; set'}
}
var result = myDbContext.Users
.Where(user => user.Id == AureId)
.Select( user => new
{
// from the user take only the data you plan to use
...
UserRoles = user.UserRoleDataModels
.Where(role => role... == ...)
.Select(role => new
{
RoleDefinition = role.RoleDefinitiona,
...
}),
});
If your Role has zero or more RoleDefinitions, do the same as you did with Users and Roles: add the virtual ICollection and add the virtual Reference and foreign key and use the collections in a Select statement.

How can I manually join cached Entity Framework objects?

I'm having a performance issue with lookups using the navigation properties of an EF model.
My model is something like this (conceptually):
public class Company
{
public int ID { get; set; }
public string CompanyName { get; set; }
public EntityCollection<Employee> Employees { get; set; }
}
public class Employee
{
public int CompanyID { get; set; }
public string EmployeeName { get; set; }
public EntityReference<Company> CompanyReference { get; set; }
}
Now let's say I want to get a list of all Companies that have (known) Employees.
Additionally, assume that I've already cached lists of the both the Companies and the Employees through previous calls:
var dbContext = new EmploymentContext();
var allCompanies = dbContext.Companies.ToList();
var allEmployees = dbContext.Employees.ToList();
bool activeCompanies =
allCompanies.Where(company => company.Employees.Any()).ToList();
This (in my environment) generates a new SQL statement for each .Any() call, following the Employees navigation property.
I already have all the records I need in my cached lists, but they're not 'connected' to each other on the client side.
I realize I can add .Include() calls to my initial cache-fill statement. I want to avoid doing this because in my actual environment I have a large number of relations and a large number of lists I'm populating up front. I'm caching largely to keep Linq from generating overly-complicated nested SQL statements that tend to bog down my database server.
I also realize I can modify my query so as to do an in-memory join:
bool activeCompanies = allCompanies.Where
(
company => allEmployees.Any(employee => employee.CompanyID == company.ID)
);
I'm trying to avoid doing such a rewrite, because the actual business logic gets rather involved. Using Linq statements has significantly improved the readability of this logic, and I'd prefer not to lose that if at all possible.
So my question is this: can I connect them together manually somehow, in the way that the Entity Framework would connect them?
I'd like to continue to use the .Any() operator, but I want it to examine only the objects I have in memory in my dbContext - without going back to the database repeatedly.

Linq selecting from muliple tables

I have the following model
public class SummaryModel
{
public int CompanyCount { get; set; }
public int GroupCount { get; set; }
public int ProjectCount { get; set; }
public int ResourcesCount { get; set; }
public int PeopleCount { get; set; }
}
I would like to use linq to query my database and return record counts from multiple tables and populate this model object.
This is how I am doing it:
using (var ctx = new WeWorkModel.weWorkEntities())
{
var summary = new SummaryModel()
{
CompanyCount = ctx.Companies.Count(),
PeopleCount = ctx.People.Count(),
GroupCount = ctx.Groups.Count(),
ProjectCount = ctx.Projects.Count(),
ResourcesCount = ctx.Resources.Count()
};
}
Is this the most efficient way to do this?
Yes, this is the most efficient way - equivalent to writing sql query as this does not fetch the objects but only does a count on the server. So something like this ( using profiler I tracked the query)
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[Company] AS [Extent1]
) AS [GroupBy1]
Do you need to store this model in a database or change it's values after instantiating? If no, why not put this code block inside of a parameterless constructor and mark the fields readonly as to avoid using this model differently than intended. If you find later you need greater control over initialization of fields, simply add another constructor to deal with that specific case. To the main question, I see nothing particularly inefficient with your way of handling it. Although, with code there is nearly always terser ways or more efficient ways of handling just about any scenario.

Linq Expressions on Child entities

I am building dynamic linq expressions which is working fine for a single entity.
For example:
I have a class called Employee and empeduinfo
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
public class EmpEduInfo
{
public int Id { get; set; }
public string Name { get; set; }
public int EmpId { get; set; }
}
I need to get all the the employees and empeduinfo class starts with "x"
I prepared expression for startswith("x")
var temp= entities.employees.Include("EmpEduInfo").Where(mydynamicexpression);
In this case it is filtering only parent table not on child.
I need to prepare generic expression so than i need to filter both parent and child objects dynamically.
Without using expression I know a solution:
var temp= (from ee in entities.Employee.Include("EmpEduInfo").Where(x => x.name.StartsWith("t"))
where ee.EmpEduInfo.Where(x => x.name.StartsWith("t")).Count()>0
select ee).ToList();
using expressions I am building generic expression to provide dynamic advance search rather than writing in each and every entity.
Here is my expression details
// Get the method information for the String.StartsWith() method
MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
// Build the parameter for the expression
ParameterExpression empparam= Expression.Parameter(typeof(employee), "ename");;
// Build the member that was specified for the expression
MemberExpression field = Expression.PropertyOrField(empparam, "name");
// Call the String.StartsWith() method on the member
MethodCallExpression startsWith = Expression.Call(field, mi, Expression.Constant("t"));
var namelamda = Expression.Lambda<Func<employee, bool>>(startsWith, new ParameterExpression[] { empparam });
var temp = entities.employees.Include("empedudetails").Where(namelamda).ToList();
You can look at the Expression the compiler generates using IQueryable:
IQueryable<Employee> query =
from ee in entities.Employee ...
var expression = query.Expression;
Look at expression in a debugger to see what you need to generate - LINQPad is good for this.
You might want to simplify your query a bit first:
IQueryable<Employee> query =
from ee in entities.Employee.Include("EmpEduInfo")
where
ee.name.StartsWith("t") &&
ee.EmpEduInfo.Any(x => x.name.StartsWith("t"))
select ee;
I was trying in EF 4.0 either we have write DB extentions for the same.
Option is provided in EF 4.1
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application
Thanks.

LinqToSQl and the Member access not legal on type exception

The basic problem...
I have a method which executes the following code:
IList<Gig> gigs = GetGigs().WithArtist(artistId).ToList();
The GetGigs() method gets Gigs from my database via LinqToSql...
So, when GetGigs().WithArtist(artistId).ToList() is executed I get the following exception:
Member access 'ListenTo.Shared.DO.Artist Artist' of 'ListenTo.Shared.DO.Act' not legal on type 'System.Collections.Generic.List`1[ListenTo.Shared.DO.Act]
Note that the extension function "WithArtist" looks like this:
public static IQueryable<Gig> WithArtist(this IQueryable<Gig> qry, Guid artistId)
{
return from gig in qry
where gig.Acts.Any(act => (null != act.Artist) && (act.Artist.ID == artistId))
orderby gig.StartDate
select gig;
}
If I replace the GetGigs() method with a method that constructs a collection of gigs in code (rather than from the DB via LinqToSQL) I do NOT get the exception.
So I'm fairly sure the problem is with my LinqToSQl code rather than the object structure.
However, I have NO IDEA why the LinqToSQl version isnt working, so I've included all the associated code below. Any help would be VERY gratefully receivced!!
The LinqToSQL code....
public IQueryable<ListenTo.Shared.DO.Gig> GetGigs()
{
return from g in DBContext.Gigs
let acts = GetActs(g.ID)
join venue in DBContext.Venues on g.VenueID equals venue.ID
select new ListenTo.Shared.DO.Gig
{
ID = g.ID,
Name = g.Name,
Acts = new List<ListenTo.Shared.DO.Act>(acts),
Description = g.Description,
StartDate = g.Date,
EndDate = g.EndDate,
IsDeleted = g.IsDeleted,
Created = g.Created,
TicketPrice = g.TicketPrice,
Venue = new ListenTo.Shared.DO.Venue {
ID = venue.ID,
Name = venue.Name,
Address = venue.Address,
Telephone = venue.Telephone,
URL = venue.Website
}
};
}
IQueryable<ListenTo.Shared.DO.Act> GetActs()
{
return from a in DBContext.Acts
join artist in DBContext.Artists on a.ArtistID equals artist.ID into art
from artist in art.DefaultIfEmpty()
select new ListenTo.Shared.DO.Act
{
ID = a.ID,
Name = a.Name,
Artist = artist == null ? null : new Shared.DO.Artist
{
ID = artist.ID,
Name = artist.Name
},
GigId = a.GigID
};
}
IQueryable<ListenTo.Shared.DO.Act> GetActs(Guid gigId)
{
return GetActs().WithGigID(gigId);
}
I have included the code for the Act, Artist and Gig objects below:
public class Gig : BaseDO
{
#region Accessors
public Venue Venue
{
get;
set;
}
public System.Nullable<DateTime> EndDate
{
get;
set;
}
public DateTime StartDate
{
get;
set;
}
public string Name
{
get;
set;
}
public string Description
{
get;
set;
}
public string TicketPrice
{
get;
set;
}
/// <summary>
/// The Act object does not exist outside the context of the Gig, therefore,
/// the full act object is loaded here.
/// </summary>
public IList<Act> Acts
{
get;
set;
}
#endregion
}
public class Act : BaseDO
{
public Guid GigId { get; set; }
public string Name { get; set; }
public Artist Artist { get; set; }
}
public class Artist : BaseDO
{
public string Name { get; set; }
public string Profile { get; set; }
public DateTime Formed { get; set; }
public Style Style { get; set; }
public Town Town { get; set; }
public string OfficalWebsiteURL { get; set; }
public string ProfileAddress { get; set; }
public string Email { get; set; }
public ImageMetaData ProfileImage { get; set; }
}
public class BaseDO: IDO
{
#region Properties
private Guid _id;
#endregion
#region IDO Members
public Guid ID
{
get
{
return this._id;
}
set
{
this._id = value;
}
}
}
}
I think the problem is the 'let' statement in GetGigs. Using 'let' means that you define a part of the final query separately from the main set to fetch. the problem is that 'let', if it's not a scalar, results in a nested query. Nested queries are not really Linq to sql's strongest point as they're executed deferred as well. In your query, you place the results of the nested query into the projection of the main set to return which is then further appended with linq operators.
When THAT happens, the nested query is buried deeper into the query which will be executed, and this leads to a situation where the nested query isn't in the outer projection of the query to execute and thus has to be merged into the SQL query ran onto the DB. This is not doable, as it's a nested query in a projection nested inside the main sql query and SQL doesn't have a concept like 'nested query in a projection', as you can't fetch a set of elements inside a projection in SQL, only scalars.
I had the same issue and what seemed to do the trick for me was separating out an inline static method call that returned IQueryable<> so that I stored this deferred query into a variable and referenced that.
I think this is a bug in Linq to SQL but at least there is a reasonable workaround. I haven't tested this out yet but my assumption is that this problem may arise only when referencing static methods of a different class within a query expression regardless of whether the return type of that function is IQueryable<>. So maybe it's the class that holds the method that is at the root of the problem. Like I said, I haven't been able to confirm this but it may be worth investigating.
UPDATE: Just in case the solution isn't clear I wanted to point it out in context of the example from the original post.
public IQueryable<ListenTo.Shared.DO.Gig> GetGigs()
{
var acts = GetActs(g.ID); // Don't worry this call is deferred
return from g in DBContext.Gigs
join venue in DBContext.Venues on g.VenueID equals venue.ID
select new ListenTo.Shared.DO.Gig
{
ID = g.ID,
Name = g.Name,
Acts = new List<ListenTo.Shared.DO.Act>(acts),
Description = g.Description,
StartDate = g.Date,
EndDate = g.EndDate,
IsDeleted = g.IsDeleted,
Created = g.Created,
TicketPrice = g.TicketPrice,
Venue = new ListenTo.Shared.DO.Venue {
ID = venue.ID,
Name = venue.Name,
Address = venue.Address,
Telephone = venue.Telephone,
URL = venue.Website
}
};
}
Note that while this should correct the issue at hand there also seems to be another issue in that the deferred acts query is being accessed in each element of the projection which I would guess would cause separate queries to be issued to the database per row in the outer projection.
I don't see anything in your classes to indicate how LINQ to SQL is meant to work out which column is which, etc.
Were you expecting the WithArtist method to be executed in .NET, or converted into SQL? If you expect it to be converted into SQL, you'll need to decorate your Gig class with appropriate LINQ to SQL attributes (or configure your data context some other way). If you want it to be executed in code, just change the first parameter type from IQueryable<Gig> to IEnumerable<Gig>.
I found out that an issue like this (which I also had recently) can be resolved, if you convert the IQueryable (or Table) variable Gigs into a list like so
return from g in DBContext.Gigs.ToList()
...
If that still doesn't work, do the same for all the IQueryables. The reason behind seems to me that some queries are too complex to be translated into SQL. But if you "materialize" it into a list, you can do every kind of query.
Be careful, you should add "filters" (where conditions) early because too much memory consumption can become a problem.

Resources