Restricting deeply-nested child records returned using LINQ - linq

Given this object structure, how do I bring back a WorkItem, it's report(s), row(s) and student(s), but only the row whose Student's name is 'Bob' (I want to omit the rows containing 'Alice' and 'Claire').
WorkItem
----Report1 (held in WorkItem.Reports collection)
--------ReportRow1 (held in Report.ReportRows collection)
------------Student.Name = 'Alice'
--------ReportRow2 (held in Report.ReportRows collection)
------------Student.Name = 'Bob'
--------ReportRow3 (held in Report.ReportRows collection)
------------Student.Name = 'Claire'
(sorry about the formatting)
I thought something like this would work, but it still brings back all 3 rows
WorkItem found = (from workItem in session.Query<WorkItem>()
from report in workItem.Reports
from row in report.ReportRows
where workItem.Id == 1 && row.Student.Name == "Bob"
select workItem)
.SingleOrDefault<WorkItem>();
Update
I also tried this, thinking it would only bring back the results when I actually try to use them (which it does) but looking at the logs, it still does a select for each student (I was hoping the 'where' clause in the final foreach loop would bring back just the one I was interested in.
var query = from workItem in session.Query<WorkItem>()
where workItem.Id == 1
select workItem;
WorkItem found = query.SingleOrDefault<WorkItem>();
foreach (var report in found.Reports)
{
foreach (var row in report.ReportRows.Where(x => x.Student.Name == "Bob"))
{
Console.WriteLine("--" + row.Student.Name);
}
}
This is my current best stab at it based on help from Ocelot20:
var result = (from workItem in session.Query<WorkItem>()
.FetchMany(x => x.Reports)
.ThenFetchMany(y => y.ReportRows.Where(z => z.Student.Name == "Bob"))
where workItem.Id == 1
select workItem);
The only thing that doesn't work is the Where clause for the student name. If I remove that, I get a result back (albeit with too many rows). If I can get the where clause right I think it will be bringing back what I want

From what I can see here, you are only selecting a WorkItem. You are doing joins that limit the WorkItems that are returned, but you are still only selecting a WorkItem and not telling it to select specific Reports, ReportRows, etc. So essentially your query is saying: "Give me only WorkItems with an Id of 1 that can be joined with students named Bob". Note the lack of: "Then select that WorkItem with only the appropriate ReportRows.
My guess is that you're doing something like this:
WorkItem found = (from workItem in session.Query<WorkItem>()
from report in workItem.Reports
from row in report.ReportRows
where workItem.Id == 1 && row.Student.Name == "Bob"
select workItem).SingleOrDefault<WorkItem>();
// Doing something to select `ReportRows` without filtering them:
var someSelection = found.Reports.First().ReportRows;
Depending on how you have lazy loading set up, ReportRows will not even be queried until you call it on the someSelection line. At this point, it knows nothing about what you want to filter on. You have a couple of options here. First, you can just filter the items in the second query once you have already loaded the WorkItem like so:
// Filter on the second query:
var someSelection = found.Reports.First().ReportRows
.Where(rr => rr.Student.Name == "Bob");
Or you could just change up your query to explicitly select the work item and related rows: select new { workItem, reportRows = // select specific rows with a where clause here. }.
Lastly, there's the nhibernate ability to specify what related entities should be preloaded into your selected entity. I know entity framework lets you add Where clauses on the related entities, but I haven't used nhibernate to know whether or not you can do something like this:
var customers = session.Query<Customer>()
.FetchMany(c => c.Orders.Where(o => o.Amount == 100);
If this works, this could be applied to your query to tell nhibernate to load the WorkItem with related rows where the Student name is "Bob".

Related

Using the result of one LINQ query as the parameter for the next

I'm trying to use the result of one LINQ query to be the parameter for the next part of a LINQ query, but to keep it all within one overall query.
For example, my (not-working) code looks like this
List<Categories> myCats = (from city in myCities
where city.CityName == myCityName
select city
from ids in city.IDs
where ids != 4
select ids).ToList();
Can this feed the result to be the be start of the next query be performed in this fashion and if it can, what am I missing to get this to work?
city.IDs is an int array
Unless you're actually trying to create a new projection, you can simply avoid using a select until the end.
List<Categories> myCats = (from city in myCities
where city.CityName == myCityName
//select city
from ids in city.IDs
where ids != 4
select ids).ToList();
I personally like breaking up my queries into pieces:
var matchingCities = from city in myCities
where city.CityName == myCityName
select city;
var matchingCityIds = (from city in matchingCities
from id in city.IDs
select id).ToList();
This approach has two main advantages:
The variable names give you automatic "documentation", allowing other developers to see what the intent of each transformation is.
It's easier to debug because you can step over each transformation and verify that you got the results you wanted.
If you really do need to follow one select by another, though, you can use the into keyword to chain the queries together.
List<Categories> myCats = (from city in myCities
where city.CityName == myCityName
// Not typically recommended
select city.IDs into cityIDs
from id in cityIDs
where id != 4
select id).ToList();

Linq data structuring

I have two issues I'm struggling with LINQ. I appreciate if you could advise. I have 2 lists rawStates (storing rows of entity-downtime-uptime-eventtype) and rawData list storing products' in and out times from entity.
I want to select those elements from rawStates that occurred when still waiting for that entity to be processed
foreach(var t in rawData)
var s = rawStates
//I am not sure if this single logic clause in Where is enough;
.Where(o => o.Entity == t.Entity
&& o.DownDate > t.InTime
&& o.Update < t.OutTime)
.ToList();
If I group my rawData by productID (there are multiple rows with same ProductID), how can I revert back this "s" to these groups so that for a productID I can group by eventtype, and summarise durations by productID?

Linq and Lambda expression for a complex sql query involving joins

Using Linq to Entity (Entity Framework) in MVC 3 project.
My model:
Table - Users
UserID (PK)
...
Table - Clients
ClientID (PK)
Table - PropertyItems
PropertyItemID (PK)
Table - MemberContactPreference (Contains PropertyItems selected by Users- many to many)
UserID(FK)
PropertyItemID(FK)
Table ClientProperties (Contains PropertyItems that belong to Clients - many to many)
ClientID (FK)
PropertyItemID (FK)
I want to list all the distinct users that have selected all the properties selected by clients.
My Approach :
I got a list of all properties for a particular client in
Iqueryable<ClientProperty> clientProperties = GetClientProperties(ClientID)
Iqueryable<User> UsersMatchingClientProperties = GetAllUsers();
foreach (ClientProperty property in clientproperties)
{
UsersMatchingClientProperties = (from uem in UsersMatchingClientProperties
join ucp in GetAllMemberContactPreferences on
ucp.UserID == uem.UserID
where uem.MemberContactPreferences.SelectMany(
mcp => mcp.PropertyItemID == property.PropertyItemID)
select uem).Distinct;
}
It gives the right result only first time. As it doesn't reduce the number of items in UsersMatchingClientProperties with each iteration. actually it replaces the collection with new resultset. I want to filter out this collection with each iteration.
Also, any suggestions to do this in Lambda expression without using Linq.
Thanks
That generation of an iqueryable in a for loop seems like a dangerous thing, which could end up in a monster sql join being executed at once.
Anyway, I don't think you need that. How about something like this?
// for a given client, find all users
// that selected ALL properties this client also selected
Iqueryable<ClientProperty> clientProperties = GetClientProperties(ClientID)
Iqueryable<User> allUsers= GetAllUsers();
Iqueryable<MemberContactPreference> allMemberContactProperties = GetAllMemberContactPreferences();
Iqueryable<User> UsersMatchingClientProperties = allUsers
.Where(user => allMemberContactProperties
.Where(membP => membP.UserID==user.UserID)
.All(membP => clientProperties
.Select(clientP => clientP.PropertyID)
.Contains(membP.PropertyID)
)
);
Here is an alternative query in case you want the users that selected ANY property for a given client
// for a given client, find all users
// that selected ANY properties this client also selected
Iqueryable<ClientProperty> clientProperties = GetClientProperties(ClientID)
Iqueryable<User> allUsers= GetAllUsers();
Iqueryable<MemberContactPreference> allMemberContactProperties = GetAllMemberContactPreferences();
Iqueryable<User> UsersMatchingClientProperties = clientproperties
.Join(allMembersContactProperties, // join clientproperties with memberproperties
clientP => clientP.PropertyItemID,
membP => membP.PropertyItemID,
(clientP, membP) => membP)) // after the join, ignore the clientproperties, keeping only memberproperties
.Distinct() // distinct is optional here. but perhaps faster with it?
.Join(allUsers, //join memberproperties with users
membP => membP.UserID,
user => user.UserID,
(membP, user) => user)) // after the join, ignore the member properties, keeping only users
.Distinct();
I trust Hugo did a good job suggesting ways to improve your query (+1). But that does not yet explain the cause of your problem, which is the modified closure pitfall.
I think that after your loop there is some code that actually executes the query in UsersMatchingClientProperties. At that moment the query is executed with the last value of the loop variable property! (The loop variable is the closure in each query delegate that is created in an iteration, and it is modified by each iteration).
Change the loop like this:
foreach (ClientProperty property in clientproperties)
{
var property1 = property;
...
and use property1 in the query. That should solve the cause of the problem. But as said, it looks like the whole process can be improved.

Wait for DomainContext.Load<t> from an entityquery with joins to complete (returning new type via 'select new')

My app consolidates data from other DBs for reporting purposes. We can't link the databases, so all the data processing has to be done in code - this is fine as we want to allow manual validation during the imports.
Certain users will be able to start an update through the Silverlight 4 front end.
I have 3 tables in database x that are fed from one EF4 Model (ModelX). I want to join those tables together, select specific columns and return the result as a new entity that exists in a different EF4 Model (ModelY). I'm using this query:
var myQuery = from i in DBx.table1 from it in DBx.table2 from h in DBx.table3 where (i.id==it.id && h.otherid == i.otherid) select new ModelYServer {Name = i.name,Thing = it.thing, Stuff = h.stuff};
The bit i'm stuck on, is how to execute that query, and wait until the Asynchronous call has completed. Normally, i'd use:
DomainContext.Load<T>(myQuery).Completed += (sender,args) =>
{List<T> myList = ((LoadOperation<T>)sender.Entities.ToList();};
but I can't pass myQuery (an IEnumerable) into the DomainContext.Load() as that expects an EntityQuery. The dataset is very large, and is taking up to 30 seconds to return, so I definitely need to wait before continuing.
So can anyone tell me how I can wait for the IEnumerable query to complete, or suggest a better way of doing this (there very likely is one).
Thanks
Mick
One simple way is just to force it to evaluate by calling ToList:
var query = from i in DBx.table1
join it in DBx.table2 on i.id equals it.id
join h in DBx.table3 on i.otherid equals h.otherid
select new ModelYServer {
Name = i.name,
Thing = it.thing,
Stuff = h.stuff
};
// This will block until the results have been fetched
var results = query.ToList();
// Now use results...
(I've changed your where clause into joins on the earlier tables, as that's what you were effectively doing and this is more idiomatic, IMO.)

Help required to optimize LINQ query

I am looking to optimize my LINQ query because although it works right, the SQL it generates is convoluted and inefficient...
Basically, I am looking to select customers (as CustomerDisplay objects) who ordered the required product (reqdProdId), and are registered with a credit card number (stored as a row in RegisteredCustomer table with a foreign key CustId)
var q = from cust in db.Customers
join regCust in db.RegisteredCustomers on cust.ID equals regCust.CustId
where cust.CustomerProducts.Any(co => co.ProductID == reqdProdId)
where regCust.CreditCardNumber != null && regCust.Authorized == true
select new CustomerDisplay
{
Id = cust.Id,
Name = cust.Person.DisplayName,
RegNumber = cust.RegNumber
};
As an overview, a Customer has a corresponding Person which has the Name; PersonID is a foreign key in Customer table.
If I look at the SQL generated, I see all columns being selected from the Person table. Fyi, DisplayName is an extension method which uses Customer.FirstName and LastName. Any ideas how I can limit the columns from Person?
Secondly, I want to get rid of the Any clause (and use a sub-query) to select all other CustomerIds who have the required ProductID, because it (understandably) generates an Exists clause.
As you may know, LINQ has a known issue with junction tables, so I cannot just do a cust.CustomerProducts.Products.
How can I select all Customers in the junction table with the required ProductID?
Any help/advice is appreciated.
The first step is to start your query from CustomerProducts (as Alex Said):
IQueryable<CustomerDisplay> myCustDisplay =
from custProd in db.CustomerProducts
join regCust in db.RegisteredCustomers
on custProd.Customer.ID equals regCust.CustId
where
custProd.ProductID == reqProdId
&& regCust.CreditCardNumber != null
&& regCust.Authorized == true
select new CustomerDisplay
{
Id = cust.Id,
Name = cust.Person.Name,
RegNumber = cust.RegNumber
};
This will simplify your syntax and hopefully result in a better execution plan.
Next, you should consider creating a foreign key relationship between Customers and RegisteredCustomers. This would result in a query that looked like this:
IQueryable<CustomerDisplay> myCustDisplay =
from custProd in db.CustomerProducts
where
custProd.ProductID == reqProdId
&& custProd.Customer.RegisteredCustomer.CreditCardNumber != null
&& custProd.Customer.RegisteredCustomer.Authorized == true
select new CustomerDisplay
{
Id = cust.Id,
Name = cust.Person.Name,
RegNumber = cust.RegNumber
};
Finally, for optimum speed, have LINQ compile your query at compile time, rather than run time by using a compiled query:
Func<MyDataContext, SearchParameters, IQueryable<CustomerDisplay>>
GetCustWithProd =
System.Data.Linq.CompiledQuery.Compile(
(MyDataContext db, SearchParameters myParams) =>
from custProd in db.CustomerProducts
where
custProd.ProductID == myParams.reqProdId
&& custProd.Customer.RegisteredCustomer.CreditCardNumber != null
&& custProd.Customer.RegisteredCustomer.Authorized == true
select new CustomerDisplay
{
Id = cust.Id,
Name = cust.Person.Name,
RegNumber = cust.RegNumber
};
);
You can call the compiled query like this:
IQueryable<CustomerDisplay> myCustDisplay = GetCustWithProd(db, myParams);
I'd suggest starting your query from the product in question, e.g. something like:
from cp in db.CustomerProducts
join .....
where cp.ProductID == reqdProdID
As you have found, using a property defined as an extension function or in a partial class will require that the entire object is hydrated first and then the select projection is done on the client side because the server has no knowledge of these additional properties. Be glad that your code ran at all. If you were to use the non-mapped value elsewhere in your query (other than in the projection), you would likely see a run-time exception. You can see this if you try to use the Customer.Person.DisplayName property in a Where clause. As you have found, the fix is to do the string concatenation in the projection clause directly.
Lame Duck, I think there is a bug in your code as the cust variable used in your select clause isn't declared elsewhere as a source local variable (in the from clauses).

Resources