How to utilize "IN" statement with Linq <> ASP.NET MVC <> C# - c#

I am having struggles finding the best method to implement an IN statement.
Currently I am using the below code in my controller to return a list of stances specific an individual customer account.
return _context.Stances
.ToList()
.Select(Mapper.Map<Stances, StancesDto>)
.Where(c => c.AccountGUID == userAccountID.CustomerGUID);
I am in the process of creating a partner portal that would allow a Partner user to have access to many other customer accounts that each individual customer provides them access to.
I setup a partnerLink table that stores a PartnerGUID and a CustomerGUID, marrying the relationship. The struggle I have is finding a method to allow a one to many relationship using an "IN" or "contains" option.
What I am looking to do is something like this:
either load the list if it is just a customer "OR" if it is a partner account Load all customer stances in the partnerLink table.
var partners = _context.PartnerLinks
.Where(user => user.PartnerGUID == userAccountID.CustomerGUID)
.Select(user => user.AccountGUID) // extract the emails from users
.ToList();
return _context.Stances
.ToList()
.Select(Mapper.Map<Stances, StancesDto>)
.Where(c => c.AccountGUID == userAccountID.CustomerGUID || partners.Contains(c.AccountGUID));

You should not use ToList when combining LINQ queries. Everything should be IQueryable.
var partners = _context.PartnerLinks
.Where(user => user.PartnerGUID == userAccountID.CustomerGUID)
.Select(user => user.AccountGUID);
return _context.Stances
.Where(c => c.AccountGUID == userAccountID.CustomerGUID || partners.Contains(c.AccountGUID))
.AsEnumerable()
.Select(Mapper.Map<Stances, StancesDto>);
Also consider to use Automapper's ProjectTo
return _context.Stances
.Where(c => c.AccountGUID == userAccountID.CustomerGUID || partners.Contains(c.AccountGUID))
.ProjectTo<StancesDto>(configuration);

I would think you could do it with a Union.
Sometime like:
return _context.Stances.Select(Mapper.Map<Stances, StancesDto>).Where(c => c.AccountGUID == userAccountID.CustomerGUID)
.Union(_context.Stances.Select(Mapper.Map<Stances, StancesDto>).Where(c => partners.Contains(c.AccountGUID)));

Related

How can I order in Linq Select Distinct if ordered by an Included entity?

I know that in Linq I have to do the OrderBy after doing a Select - Distinct, but I'm trying to order by an Included entity property that get lost after the Select.
For example:
var accounts = _context.AccountUser
.Include(o => o.Account)
.Where(o => o.UserId == userId || o.Account.OwnerId == userId)
.OrderByDescending(o => o.LastAccessed)
.Select(o => o.Account)
.Distinct();
As I'm doing the Where by an or of two different parameters, there is a good chance to obtain duplicated results. That's why I'm using the Distinct.
The problem here is that after I do the Select, I don't have the LastAccessed property anymore because it doesn't belong to the selected entity.
I thing the structure of the AccountUser and Account can be inferred from the query itself.
If you have the bi-directional navigation properties set up:
var accountsQuery = _context.AccountUser
.Where(o => o.UserId == userId || o.Account.OwnerId == userId)
.Select(o => o.Account)
.Distinct()
.OrderByDescending(a => a.AccountUser.LastAccessed);
When Selecting the Account you do not need .Include() Keep in mind that any related entities that you access off the Account will be lazy-loaded. I recommend using a .Select() to extract either a flattened view model or a view model hierarchy so that the SQL loads all needed fields rather than either eager-loading everything or tripping lazy-load calls.
Since LINQ doesn't implement DistinctBy and LINQ to SQL doesn't implement Distinct that takes an IEqualityComparer, you must substiture GroupBy+Select instead:
var accounts = _context.AccountUser
.Include(o => o.Account)
.Where(o => o.UserId == userId || o.Account.OwnerId == userId)
.GroupBy(o => o.Account).Select(og => og.First())
.OrderByDescending(o => o.LastAccessed)
.Select(o => o.Account);

Linq - Trying to filter out the Eager Selection

I am trying to filter out the second part of the tables (UserRoles.IsDeleted==false). Is there any advice how i can do that?
var Users = context.Users.Where(r => r.IsDeleted == IsDeleted).ToList<User>();
Users = context.Users.Include(x => x.UserRoles.Select(y=>y.IsDeleted==false)).ToList();
Thank you
You can do the following to filter using the second part:
var Users = context.Users.Where(r => r.IsDeleted == IsDeleted).ToList<User>();
if(condition)
{
Users = Users.where(y => y.IsDeleted == false)).ToList();
}
There are two options to filter related entities
Doing a projection.
Unfortunately, when you use Include method, you can't filter the related entities as you intend to do. You need to project your query to a DTO object or a anonymous object, as the below example.
var query=context.Users.Include(x => x.UserRoles)
.Where(r => r.IsDeleted == IsDeleted)
.Select(u=> new{ ...,
Roles=x => x.UserRoles.Where(y=>!y.IsDeleted)})
A second option could be using Explicitly Loading. But this is in case you can load the related entities of one specific entity,eg,.
var user=context.Users.FirstOrDefault(r.IsDeleted == IsDeleted);//Getting a user
context.Entry(user)
.Collection(b => b.UserRoles)
.Query()
.Where(y=>!y.IsDeleted)
.Load();
You can do this inside of a foreach per each entity you get from the first query,
var query=context.Users.Where(r => r.IsDeleted == IsDeleted);
foreach(var u in query)
{
context.Entry(u)
.Collection(b => b.UserRoles)
.Query()
.Where(y=>!y.IsDeleted)
.Load();
}
but it's going to be really inefficient because you are going to do a roundtrip to your DB per each entity. My advice is use the first option, projecting the query.

Get complex object using IN equivalent in LINQ

I have a list of type customer. I need to insert all values of the list in the database before checking if a customer with the same customer number exists for that particular client.
For that I am firing a query to get me all customers who are there in the database having customer number equal to ones in the list. The query I am writing is not working, here's the code.
CustomerRepository.Find(x => x.ClientId == clientId)
.Where(x => x.CustomerNumber.Contains(lstCustomersInserted.Select(c => c.CustomerNumber)));
Keep it simple:
var lstCustomerNumbers = lstCustomersInserted.Select(c => c.CustomerNumber);
var res = CustomerRepository.Where(x => x.ClientId == clientId && lstCustomerNumbers.Any(c => c == x.CustomerNumber));
I think you have it backwards. Try reversing the Contains.
Edit: I switched to using the generic predicate Exists instead of Contains based on the comment, so you can match a property.
CustomerRepository.Find(x => x.ClientId == clientId)
.Where(x => lstCustomersInserted.Exists(c => x.CustomerNumber == c.CustomerNumber));
How about an Except?
CustomerRepository.Select(x => x.ClientID)
.Except(lstCustomersInserted.Select(x => x.CustomerID));
This will return the IDs of the objects in the repo that don't exist in your lstCustomersInserted.

Merge results of 2 LINQ To SQL queries and keep their ordering

I need to append the results of one LINQ To SQL query to another on the database server side without reordering rows.
I need all errored orders first, then the pending orders.
var error = database.Orders.
Where(o => o.Status == XOrderStatus.Error).
OrderByDescending(o => o.Id);
var pending = database.Orders.
Where(o => o.Status == XOrderStatus.PendingReview).
OrderByDescending(o => o.Id);
var r = error.OrderedUnion(pending);
How I can implement the OrderedUnion() method? Concat, Union and Distinct methods completely eliminate OrderByDescending call (by design).
I know it is possible to do with two ToArray() calls, but I am interested having this done at the database level.
You can concatente them together and then order by a column that seperates the order groups. so for instance in an SQL Query you would do this:
ORDER BY XOrderStatus, Id
and that would order by ID but with the two OrderStatuses grouped.
I don't know linq to sql (sorry!) but a quick Google mentioned this may work:
.Orderby(o => o.XOrderStatus).ThenBy(o => o.Id)
Original Source:
Multiple "order by" in LINQ
I found much better solution:
var all = database.Orders.
Where(o =>
o.Status == XOrderStatus.Error ||
o.Status == XOrderStatus.PendingReview).
OrderBy(o => o.Status == XOrderStatus.Error ? 0 : 1).
ThenByDescending(o => o.Id).
ToArray();

How can I condense the following Linq query into one IQueryable

I have an EntityFramework model that has User entity that has an EntityCollection of Organisations.
For a particular User I am trying to write a Linq Query to return the names of the organisations that the user belongs where that query will hit the db only once.
My problem is that I cannot see how to write this query without having to materialise the user first then query the users organisation collection.
I would like to try and write one query that hits the db once.
What I have so far:
var orgNames = context.Users
.Where(u => u.LoweredUserName == userName.ToLower())
//materialises user
.FirstOrDefault()
.Organisations
//second hit to the db
.Select(o => o.Name);
What I was psuedo aiming for but cannot see the wood for the trees:
orgNames = context.Users
.Where(u => u.LoweredUserName == userName.ToLower())
//don't materialise but leave as IQueryable
.Take(1)
//The problem: turn what the query sees as possibly multiple
// (due to the Take method) EntityCollection<Organisation> into a List<String>
.Select(u => u.Organisations.Select(o => o.Name));
I have looked at aggregates but I seem to be going in circles :)
Doh! I think I can answer my own question by using SelectMany to condense the Collection of Collections into one collection like so:
orgNames = context.Users.Where(u => u.LoweredUserName == userName.ToLower())
.Take(1)
.SelectMany(u => u.Organisations)
.Select(o => o.Name);
I'm assuming that Lowered User name is unique, otherwise the query would be fairly meaningless, so you can just use.
context.Users
.Where(u => u.LoweredUserName == userName.ToLower())
.Select(u => u.Organisations.Select(o => o.Name));

Categories

Resources