select parent given child id - linq

A seemingly simple problem turns out more difficult than I thought:
public class SomeCategory
{
public virtual int Id { get; set; }
public virtual IList<SomeClass> SomeInstances { get; set; }
}
public class SomeClass
{
public virtual int Id { get; set; }
}
There is a 1:m relationship between SomeClass and SomeCategory (i.e. an instance of SomeClass belongs to exactly one SomeCategory and SomeCategory can have several SomeClass instances).
Question how do I get the SomeCategory given a SomeClass Id (Linq to NHibernate)?

I assume you will have access to SomeCategory list, then try
var category = someCategoryList.FirstOrDefault(e => e.SomeInstances
.Any(a => a.Id == someclassId));

You can also do it using QueryOver.
Parent parentAlias = null;
Child childAlias = null;
var query = session.QueryOver<Parent>(() => parentAlias)
.JoinAlias(()=> parent.Childs, ()=> childAlias)
.Where(()=> childAlias.Parent.Id == id)
.Select(()=> childAlias.Parent)
.SingleOrDefault();

Related

LINQ: How to filter collection by data in related table

I have two Entity Framework Core entities:
public class JobOrder {
public int Id { get; set; }
public string JobTitle { get; set; }
public string Status { get; set; }
...
public IEnumerable<JobOrderUser> JobOrderUsers { get; set; }
}
public class JobOrderUser {
public int AppUserId { get; set; }
public AppUser User { get; set; }
public int JobOrderId { get; set; }
public JobOrder JobOrder { get; set; }
}
The second one is a join table used for a many-to-many between the JobOrder and User tables but I don't need to drill to the User table for this.
I want to get a collection of JobOrders that have an association with a specific user. In SQL, I would write something like this:
select distinct
a.*
from
JobOrders a
join JobOrderUser b on a.JobOrderID = b.JobOrderId
where
b.AppUserId = someId
How do I do that using LINQ method syntax?
If your entities are set up correctly, and their relationships are intact, you could load the JobOrderUsers while querying for a JobOrder and then filter by a user. Something like
JobOrder.Include(t => t.JobOrderUsers).Where(t => t.JobOrderUsers.Any(x => x.User.Id == SomeId));
You can also use the below query using join and Select the required columns data in a jobOrdersDto class. For that, you have to inject the _jobOrdersRepository and _jobOrderUserRepository repositories to your service where you are calling the required method.
var result = (from jobOrders in _jobOrdersRepository.GetAll()
join jobOrderUser in _jobOrderUserRepository.GetAll() on jobOrders.JobOrderID equals jobOrderUser.JobOrderId
where
(jobOrderUser.AppUserId == someId)
select new jobOrdersDto
{
}
Your Service class:
public class YourService
{
private readonly IRepository<jobOrders> _jobOrdersRepository;
private readonly IRepository<jobOrderUser> _jobOrderUserRepository;
public YourService(
IRepository<jobOrders> jobOrdersRepository, IRepository<jobOrderUser> jobOrderUserRepository)
: base()
{
_jobOrdersRepository = jobOrdersRepository;
_jobOrderUserRepository = jobOrderUserRepository;
}
}

Retrieving information from derived child object collections using LINQ

I have been trying to get a list of all Workflows that have Offices contained in a certain List by Office Id. I can easily get all of the Workflows that have SingleWorkflowSteps because they have only one Office, but have been unable to understand how I would successfully get those contained in a MultiWorkflowStep. All workflow steps have either a SingleWorkflowStep or a MultiWorkflowStep that contains two or more SingleWorkflowSteps. At the time I designed this, it seemed like a logical way to do this but atlas my LINQ-fu is not as good as I thought it was. Can someone please point me in the right directions. Code listed below:
var OfficesToFind = new List<int> (new int[] { 1,3,5,7,9,10,11,12} );
public class Workflow
{
public Workflow()
{
WorkflowSteps = new List<WorkflowStepBase>();
}
public int Id { get; set; }
public virtual ICollection<WorkflowStepBase> WorkflowSteps { get; set; }
}
public abstract class WorkflowStepBase
{
public int Id { get; set; }
public int StatusId { get; set; }
public virtual Workflow Workflow { get; set; }
public virtual Status Status { get; set; }
}
public class MultiWorkflowStep : WorkflowStepBase
{
public MultiWorkflowStep()
{
ChildSteps = new List<SingleWorkflowStep>();
}
public virtual ICollection<SingleWorkflowStep> ChildSteps { get; set; }
}
public class SingleWorkflowStep : WorkflowStepBase
{
public int? ParentStepId { get; set; }
public int OfficeId { get; set; }
public virtual MultiWorkflowStep ParentStep { get; set; }
public virtual Office Office { get; set; }
}
public class Office
{
public int Id { get; set; }
public string Name { get; set; }
}
public class WorkflowService : IWorkflowService<Workflow>
{
private readonly IRepository<Workflow> _workflowService;
private readonly IRepository<SingleWorkflowStep> _singleStepService;
private readonly IRepository<MultiWorkflowStep> _multiStepService;
public WorkflowService(IUnitOfWork uow)
{
_workflowService = uow.GetRepository<Workflow>();
_singleStepService = uow.GetRepository<SingleWorkflowStep>();
_multiStepSercice = uow.GetRepository<MultiWorkflowStep>();
}
// ~ ------- Other CRUD methods here -------- ~
public IEnumerable<Workflow> GetWorkflowFilter(List<int> statuses, List<int> offices...)
{
var query = _workflowService.GetIQueryable(); // returns an IQueryable of dbset
if(statuses.Any())
{
query = query.Where(q => statuses.Contains(q.StatusId));
}
if(offices.Any())
{
// Get all active single steps and the ones that contain the offices
singleSteps = _singleStepService
.Where(s => s.StatusId == (int)Enumerations.StepStatus.ACTIVE)
.Where(s => offices.Contains(s.OfficeId));
// Get all of the parent Workflows for the singleSteps
var workflows = singleSteps.Select(w => w.Workflow);
// Update the query with the limited scope
query = query.Where(q => q.Workflow.Contains(q));
}
return query.ToList();
}
}
OK, after a good night sleep, being all bright-eyed and bushy-tailed, I figured out my own problem. First the updated code was all wrong. Because each derived WorkflowStep has access to the Workflow and each MultiWorkflowStep contains a list of SingleWorkflowSteps - when I get the list of all SingleWorkflowSteps (which would include all from MultiWorkflowStep(s)), I simply needed to get a list of all of the parent Workflows of the SingleWorkflowSteps. Next I updated my query to limit the Workflows that were contained in the new Workflow list and here is the correct code for the GetWorkflowFilter method:
...
if(offices.Any())
{
// Get all active single steps and the ones that contain the offices
singleSteps = _singleStepService.Where(s => s.StatusId == (int)Enumerations.StepStatus.ACTIVE).Where(s => offices.Contains(s.OfficeId));
// Get all of the parent Workflows for the singleSteps
var workflows = singleSteps.Select(w => w.Workflow);
// Update the query with the limited scope
query = query.Where(q => q.Workflow.Contains(q));
}
return query.ToList();
}

Query that Groups Parent, then Child, when fetching from IQueryable<GrandChild>

Lets imagine that below is our data model. I want to query the Toys but have the results returned such that I can do the following psuedo code.
foreach (var parent in parents)
{
Console.WriteLine(parent.Name);
foreach (var child in parent.Children)
{
Console.WriteLine(child.Name);
foreach (var toy in child.Toys)
{
Console.WriteLine(toy.Name);
}
}
}
The data model:
public class Parent
{
public Guid ParentId { get; set; }
public String Name { get; set; }
public ICollection<Child> Children { get; set; }
}
public class Child
{
public Guid ChildId { get; set; }
public Guid ParentId { get; set; }
public Parent Parent { get; set; }
public String Name { get; set; }
public ICollection<Toy> Toys { get; set; }
}
public class Toy
{
public Guid ToyId { get; set; }
public Guid ChildId { get; set; }
public Child Child { get; set; }
public String Name { get; set; }
public bool IsCool { get; set; }
}
I've tried doing this with grouping but when I try to iterate a grouping all that I see is Key which doesn't have any properties on it so I can't get the Parent.Name or the Child.Name.
Thanks in advance.
You can do:
var query = from p in parents
from c in p.Children
from t in c.Toys
select new { Parent = p.Name, Child = c.Name, Toy = t.Name }
foreach (var a in query)
{
// write values.
}
In effect, this uses SelectMany.
Edit
After your comment, I think what you're looking for is not a grouping. If you've got a Parent object in fact its Children are already grouped inside of it, because each parent has its own collection. But maybe you're looking for a way to populate the collections? That would be done by
var query = from p in parents.Include(x => x.Children.Select(c => c.Toys));
With LazyLoadingEnabled set to be false, you will be able to do this by using one linq query to load all related objects.
Please note, this may cause some performance issue if you have great amount of related data in Children or Toys table.
context.ContextOptions.LazyLoadingEnabled = false;
var parents = context.Parents.Select(x=>x).ToList();
foreach(var parent in parents) {
Console.WriteLine(parent.Name);
foreach(var child in parent.Children) {
Console.WriteLine(child.Name);
foreach(var toy in child.Toys) {
Console.WriteLine(toy.Name);
}
}
}

Linq extension to recursively retrieve collection of items

public class MyClass
{
public MyClass();
public long Id { get; set; }
public intScore { get; set; }
public MyClass[] subclasses { get; set; }
public string title { get; set; }
.....
}
The results returned from extrenal source are recursive, I am trying to retrieve the results from the collection using linq or any extension methods using recursion, any help appreciated.
ex; The collection i have is
Myclass results=XXXmethod(xxxx)// which gives me results.
subclasses is a list of type Myclass, so this list will have again some collection, and inturn that can collection agian nested levels
say the service returned 10 results or Myclass[10] Myclass[0] is having again Myclass[4] which might have 2 or 4, need to build a collection allitems
I am trying to do like this but some are missing
results.class.subclusters.subclasses (o => o.subclasses )
.SelectMany(x => x.subclasses ).ToList()
but which is not giving correct results.
LINQ itself does not support recursive method, but you can have the recursive method like below:
public class MyClass
{
public int Id { get; set; }
public MyClass[] SubClasses { get; set; }
//More properties
public IEnumerable<MyClass> GetRecursive()
{
yield return this;
if (SubClasses != null)
{
foreach (var item in SubClasses
.Where(s => s != null)
.SelectMany(x => x.GetRecursive()))
{
yield return item;
}
}
}
}
Then call:
myClass.GetRecursive()

Linq to entities: cast result to multiple listed complex types

I'm trying to create a single linq query which populates the following models in the CompanyViewModel constructor:
public class CompanyViewModel
{
public IList<CompanyUserViewModel> CompanyUsers { get; set; }
...
}
public class CompanyUserViewModel
{
public User User { get; set; }
public IList<UserOperationViewModel> UsersOperations { get; set; }
}
public class UserOperationViewModel
{
public Operation Operation { get; set; }
public int Permission { get; set; }
}
Currently I've got the following query:
return db.Users.Where(u => u.CompanyId == companyId)
.Select(u => new CompanyUserViewModel {
User = u,
UsersOperations = db.UsersInOperations
.Where(uo => uo.UserId == uo.UserId)
.Select(uo => new UserOperationViewModel{
Operation = uo.Operation,
Permission = uo.Permission
}).ToList()
}).ToList();
Which builds, but when the page runs I get
LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[WoodCo.Models.BusinessObject.UserOperationViewModel] ToList[UserOperationViewModel](System.Collections.Generic.IEnumerable`1[WoodCo.Models.BusinessObject.UserOperationViewModel])' method, and this method cannot be translated into a store expression.
What does one do?
Change your view model properties to use IEnumerable<T> instead of IList<T and remove the .ToList() calls.

Resources