fluent nhibernate outer join 2 query - c#

I am using linq-to-nhibernate with fluent (c#), and I want to do the following:
I have two IQueryable interfaces, relations and documentsrelations.
The relations object contain a list of enterprises and the documentsrelations object contain a second list of enterprises.
I want to generate a new list of enterprises that contains the list of enterprises (relations) minus the second list of enterprises (documentsrelations).
In sql I would try with an outer join, but I don't know how to do with this.
**** DECLARATIONS ****
IQueryable<EnterpriseRelation> documentsrelations =
shared_doc.SharedIn.AsQueryable();
var relations = EnterpriseRelationService
.QueryRelationsForEnterprise(LoggedUser.ActiveAsEnterprise)
.Where(x => x.ContactingEnterprise.NIF == LoggedUser.ActiveAsEnterprise.NIF);
relations is also an IQueryable<EnterpriseRelation>.
I tried multiplied things, but it always tells me that it is not supported.
Some help?
Thanks!

Supposing your entity primary key is Id, have you tried something like below?
var relations = EnterpriseRelationService
.QueryRelationsForEnterprise(LoggedUser.ActiveAsEnterprise)
.Where(x => x.ContactingEnterprise.NIF == LoggedUser.ActiveAsEnterprise.NIF)
.Where(x => !documentsrelations.Select(dr => dr.Id).Contains(x.Id));
Note:
Using .AsQueryable() as you do in your question is most of the time a mistake. If SharedIn is not actually a queryable instance, it will just convert it to a linq-to-object queryable, executing in memory, not in database. And for your case, there are no need to have it as a queryable anyway, at least with my answer above, and unless it is already a queryable awaiting execution in db.
If my answer fails, try again without your AsQueryable call on SharedIn.

Related

Include() vs Select() performance

I have a parent entity with a navigation property to a child entity. The parent entity may not be removed as long as there are associated records in the child entity. The child entity can contain hundreds of thousands of records.
I'm wondering what will be the most efficient to do in Entity Framework to do this:
var parentRecord = _context.Where(x => x.Id == request.Id)
.Include(x => x.ChildTable)
.FirstOrDefault();
// check if parentRecord exists
if (parentRecord.ChildTable.Any()) {
// cannot remove
}
or
var parentRecord = _context.Where(x => x.Id == request.Id)
.Select(x => new {
ParentRecord = x,
HasChildRecords = x.ChildTable.Any()
})
.FirstOrDefault();
// check if parentRecord exists
if (parentRecord.HasChildRecords) {
// cannot remove
}
The first query may include thousands of records while the second query will not, however, the second one is more complex.
Which is the best way to do this?
I would say it depens. It depends on which DBMS you're using. it depends on how good the optimizer works etc.
So one single statement with a JOIN could be far faster than a lot of SELECT statements.
In general I would say when you need the rows from your Child table use .Include(). Otherwise don't include them.
Or in simple words, just read the data you need.
The answer depends on your database design. Which columns are indexed? How much data is in table?
Include() offloads work to your C# layer, but means a more simple query. It's probably the better choice here but you should consider extracting the SQL that is generated by entity framework and running each through an optimisation check.
You can output the sql generated by entity framework to your visual studio console as note here.
This example might create a better sql query that suites your needs.

Does using AsNoTracking() make a difference when only returning fields?

So I've read a lot about using AsNoTracking() when performing a query in EF, specifically if it returns entities, as to not keep around references to things if you will not be updating.
But I've also read that AsNoTracking may also speed up the queries themselves as EF does not have to map each item queried to an entity in the map.
The question is, if my Linq query simply returns values from the columns/rows but not an entity type, does using AsNoTracking() make a difference to speed up the query? And if not obviously I shouldn't use it because it just clutters the code?
example 1 (I would expect to use AsNoTracking():
var result = (from p in context.Pogs
select p).AsNoTracking();
example 2 (My question... I'm thinking it doesn't make sense to use here, but I don't know the answer):
var result = (from p in context.Pogs
select p.Name); // assuming p.Name is a string or something
versus
var result = (from p in context.Pogs.AsNoTracking()
select p.Name);
No, it does not since the entities won't be loaded, as evidenced by examining context.Pogs.Local which won't contain the entities whose properties were retrieved through LINQ.
You can check the entities being tracked through DbContext.ChangeTracker. So if you retrieve the entries of the tracker for your Pogs DbSet through context.ChangeTracker.Entries<Pogs>() you'll see that for your first example there are entries tracking the corresponding entities, while for the second example there are none.

ADO.NET EF - using LINQ 2 Entity and GroupBy to distinct by two columns

I know the title is a little confusing but the problem is very straight forward. I am using Entity Framework and Code First approach. I have those two entities with relation 1:N. The uniqueness of the records comes from the combination of two columns - Code + CountryID. First I thought that Code itself will be unique files so my query for getting distinct records was looking like this:
IList<SoleColor> soles = SoleColorService.All()
.GroupBy(x => x.Sole.Code)
.Select(g => g.FirstOrDefault())
.ToList();
But now it occurs that doing so I lose those records where the Code (or to be exact - the Sole.Code) is the same but the CountryID is different. What I need is to change my query so it returns all records where Code + CountryID is unique.
You want to group by an anonymous type:
.GroupBy(x => new { x.Sole.Code, x.Sole.CountryID })
BTW: Using FirstOrDefault on groups returned from a GroupBy doesn't make sense. Every group returned by a GroupBy contains at least one element, so using First is guaranteed to always succeed.

Best order to place the Fetch or FetchMany in nHibernate Linq statements

If you do an nHiberante Linq query and you want to eager load the related objects.
Where do you put the Fetch or the FetchMany ?
Like this:
_session.Query<Entity>()
.Where(x => x.IsSomething)
.FetchMany(x => x.Children);
Or like this:
_session.Query<Entity>()
.FetchMany(x => x.Children)
.Where(x => x.IsSomething);
I want to know the best order to place the Fetch or FetchMany (For performance). Or does the order even matter? When I use entity framework i always write the includes first, is this the same case in nHibernate?
We use the specification pattern with nHibernate. So is it smart to put the Fetch or FetchMany in the specifications?
Doesn't matter. IQueryable is just a query statement and Fetch(), FetchMany() is just a setting on the Nhibernate query which enlarge the query to return more data so that you don't perform lazy loading later on. The query is not sent to the database until you call ToList(), Single() etc. The linq query is then transformed into a sql query which will contain more joins and columns and THEN it is sent to the database server.
The following query will do a "similar" join when fetching the entity + children but here I map it into a anonymous object:
_session
.Query<Entity>()
.Where(x => x.IsSomething)
.Select(x => new { MyEntity = x, MyEntitiesChildren = x.Children });

How do I apply the LINQ to SQL Distinct() operator to a List<T>?

I have a serious(it's getting me crazy) problem with LINQ to SQL. I am developing an ASP.NET MVC3 application using c# and Razor in Visual Studio 2010.
I have two database tables, Product and Categories:
Product(Prod_Id[primary key], other attributes)
Categories((Dept_Id, Prod_Id) [primary keys], other attributes)
Obviously Prod_Id in Categories is a foreign key. Both classes are mapped using the Entity Framework (EF). I do not mention the context of the application for simplicity.
In Categories there are multiple rows containing the Prod_Id. I want to make a projection of all Distinct Prod_Id in Categories. I did it using plain (T)SQL in SQL Server MGMT Studio according to this (really simple) query:
SELECT DISTINCT Prod_Id
FROM Categories
and the result is correct. Now I need to make this query in my application so I used:
var query = _StoreDB.Categories.Select(m => m.Prod_Id).Distinct();
I go to check the result of my query by using:
query.Select(m => m.Prod_Id);
or
foreach(var item in query)
{
item.Prod_Id;
//other instructions
}
and it does not work. First of all the Intellisense when I attempt to write query.Select(m => m. or item.shows just suggestions about methods (such as Equals, etc...) and not properties. I thought that maybe there was something wrong with Intellisense (I guess most of you many times hoped that Intellisense was wrong :-D) but when I launch the application I receive an error at runtime.
Before giving your answer keep in mind that;
I checked many forums, I tried the normal LINQ to SQL (without using lambdas) but it does not work. The fact that it works in (T)SQL means that there is something wrong with the LINQ to SQL instruction (other queries in my application work perfectly).
For application related reasons, I used a List<T> variable instead of _StoreDB.Categories and I thought that was the problem. If you can offer me a solution without using a List<T> is appreciated as well.
This line:
var query = _StoreDB.Categories.Select(m => m.Prod_Id).Distinct();
Your LINQ query most likely returns IEnumerable... of ints (judging by Select(m => m.Prod_Id)). You have list of integers, not list of entity objects. Try to print them and see what you got.
Calling _StoreDB.Categories.Select(m => m.Prod_Id) means that query will contain Prod_Id values only, not the entire entity. It would be roughly equivalent to this SQL, which selects only one column (instead of the entire row):
SELECT Prod_Id FROM Categories;
So when you iterate through query using foreach (var item in query), the type of item is probably int (or whatever your Prod_Id column is), not your entity. That's why Intellisense doesn't show the entity properties that you expect when you type "item."...
If you want all of the columns in Categories to be included in query, you don't even need to use .Select(m => m). You can just do this:
var query = _StoreDB.Categories.Distinct();
Note that if you don't explicitly pass an IEqualityComparer<T> to Distinct(), EqualityComparer<T>.Default will be used (which may or may not behave the way you want it to, depending on the type of T, whether or not it implements System.IEquatable<T>, etc.).
For more info on getting Distinct to work in situations similar to yours, take a look at this question or this question and the related discussions.
As has been explained by the other answers, the error that the OP ran into was because the result of his code was a collection of ints, not a collection of Categories.
What hasn't been answered was his question about how to use the collection of ints in a join or something in order to get at some useful data. I will attempt to do that here.
Now, I'm not really sure why the OP wanted to get a distinct list of Prod_Ids from Categories, rather than just getting the Prod_Ids from Projects. Perhaps he wanted to find out what Products are related to one or more Categories, thus any uncategorized Products would be excluded from the results. I'll assume this is the case and that the desired result is a collection of distinct Products that have associated Categories. I'll first answer the question about what to do with the Prod_Ids first, and then offer some alternatives.
We can take the collection of Prod_Ids exactly as they were created in the question as a query:
var query = _StoreDB.Categories.Select(m => m.Prod_Id).Distinct();
Then we would use join, like so:
var products = query.Join(_StoreDB.Products, id => id, p => p.Prod_Id,
(id,p) => p);
This takes the query, joins it with the Products table, specifies the keys to use, and finally says to return the Product entity from each matching set. Because we know that the Prod_Ids in query are unique (because of Distinct()) and the Prod_Ids in Products are unique (by definition because it is the primary key), we know that the results will be unique without having to call Distinct().
Now, the above will get the desired results, but it's definitely not the cleanest or simplest way to do it. If the Category entities are defined with a relational property that returns the related record from Products (which would likely be called Product), the simplest way to do what we're trying to do would be the following:
var products = _StoreDB.Categories.Select(c => c.Product).Distinct();
This gets the Product from each Category and returns a distinct collection of them.
If the Category entity doesn't have the Product relational property, then we can go back to using the Join function to get our Products.
var products = _StoreDB.Categories.Join(_StoreDB.Products, c => c.Prod_Id,
p => p.Prod_Id, (c,p) => p).Distinct();
Finally, if we aren't just wanting a simple collection of Products, then some more though would have to go into this and perhaps the simplest thing would be to handle that when iterating through the Products. Another example would be for getting a count for the number of Categories each Product belongs to. If that's the case, I would reverse the logic and start with Products, like so:
var productsWithCount = _StoreDB.Products.Select(p => new { Product = p,
NumberOfCategories = _StoreDB.Categories.Count(c => c.Prod_Id == p.Prod_Id)});
This would result in a collection of anonymous typed objects that reference the Product and the NumberOfCategories related to that Product. If we still needed to exclude any uncatorized Products, we could append .Where(r => r.NumberOfCategories > 0) before the semicolon. Of course, if the Product entity is defined with a relational property for the related Categories, you wouldn't need this because you could just take any Product and do the following:
int NumberOfCategories = product.Categories.Count();
Anyway, sorry for rambling on. I hope this proves helpful to anyone else that runs into a similar issue. ;)

Categories