Reputation: 15
I have the following classes:
public class AssignmentDetails : EntityBase
{
public DateTime StartingTime { get; protected set; }
public DateTime EndingTime { get; protected set; }
public ICollection<AssignedUser> AssignedUsers { get; protected set;}
public Assignment Assigment { get; protected set; }
public int AssigmentId { get; protected set; }
public int OwnerId { get; protected set; }
public User Owner { get; protected set; }
public State State { get; protected set; }
public AssignmentDetails(DateTime startingTime,DateTime endingTime,int ownerId,State state,ICollection<AssignedUser> assignedUsers)
{
AssignedUsers = assignedUsers;
StartingTime = startingTime;
EndingTime = endingTime;
OwnerId = ownerId;
State = state;
}
public AssignmentDetails() { }
}
public class Assignment : EntityBase
{
public string Title { get; protected set; }
public AssignmentDetails AssignmentDetails { get; protected set; }
public string Description { get; protected set; }
public int ParentAssignmentId { get; protected set; }
public Assignment(string title,string description,AssignmentDetails assignmentDetails)
{
Title = title;
Description = description;
AssignmentDetails = assignmentDetails;
}
public void AddParentAssignment(int parentAssignmentId)
{
ParentAssignmentId = parentAssignmentId;
}
public Assignment(){ }
}
public class AssignedUser : EntityBase
{
public AssignmentDetails Assignment { get; protected set; }
public int AssignmentDetailsId { get; protected set; }
public int UserId { get; protected set; }
public AssignedUser(int userId)
{
UserId = userId;
}
public AssignedUser() { }
}
I need to find the all assignments for a particular user which is Owner of a assignment, or is assigned in AssignedUsers
I tried the following and some other alternatives but looks like i catch my ears somewhere.
public async Task<List<Assignment>> GetAllAsignmentsAsync(int userId)
{
return await _context.Assignments
.Include(a => a.AssignmentDetails)
.Where(x => x.AssignmentDetails.OwnerId == userId || x.AssignmentDetails.AssignedUsers.SelectMany(u=> u.UserId== userId))
.ToListAsync();
}
Upvotes: 0
Views: 717
Reputation: 13399
.Where(x => x.AssignmentDetails.OwnerId == userId
|| x.AssignmentDetails.AssignedUsers.Any(u=> u.UserId== userId))
Use Any
to return true
or false
condition. SelectMany
selects some objects based on condition(s)
Upvotes: 5