Exclude duplicate pair from List<model, model> - c#

In result of my select
var names = _db.Messages
.Where(p => p.UserId == parameters.UserId || p.ToUserId == parameters.UserId)
.Select(p => new {p.FirstUser, p.SecondUser})
.Distinct()
.ToList();
I get four elements of type List<User, User>.
As you can see I have one duplicates
[0] and [2]. Only in sequence FirstUser and SecondUser they have difference
How can I Exclude one of them in query before ToList() ? And it will be perfect if in result I will have List<User> with all of them without duplicates.

Looks that you don't care about who send the message and who received it. This is a solution for you:
var names =
_db.Messages
.Where(p => p.UserId == parameters.UserId || p.ToUserId == parameters.UserId)
.Select(p => new {
user1 = p.UserId > p.ToUserId ? p.FirstUser.Id : p.SecondUser.Id,
user2 = p.UserId <= p.ToUserId ? p.FirstUser.Id : p.SecondUser.Id,
})
.Distinct()
.ToList();

Related

(Entity) Linq Group By statement type conversion

I am having trouble converting the list type from group by statement back to the type I can use.
I know group by adds a key to the list but how do I get rid of it and it my case convert it back to List<UserAnswers> ? Can anybody provide some insights or point me in the right direction?
public async Task<List<UserAnswers>>
GetQuestionsWellAsync(string UserName) // get questions
{
return await ctx.UserAnswers.Where(x => x.UserId == UserName && x.Correct == 1).
GroupBy(c => c.QuestionId).Where(grp => grp.Count() > 2).ToListAsync();
You can use .SelectMany() to flatten your groupings back out into a single aggregated list.
public async Task<List<UserAnswers>> GetQuestionsWellAsync(string UserName)
{
return await ctx.UserAnswers
.Where(x => x.UserId == UserName && x.Correct == 1)
.GroupBy(c => c.QuestionId)
.Where(grp => grp.Count() > 2)
.SelectMany(grp => grp)
.ToListAsync();
}
Based on your update, it seems like EF cannot translate the GroupBy + Where Count() expression. You may need to project the grouped data into memory first and then filter.
public async Task<List<UserAnswers>> GetQuestionsWellAsync(string UserName)
{
return (await userAnswers
.Where(x => x.UserId == UserName && x.Correct == 1)
.GroupBy(c => c.QuestionId)
.ToListAsync())
.Where(grp => grp.Count() > 2)
.SelectMany(grp => grp)
.ToList();
}

Convert SQL Query to LINQ Lambda C#

I have to fix a query which was already written in the LINQ Lambda, I found the fix in a Simple SQL Query but now I have some trouble in converting it to LINQ Query,
Here is my SQL Query
select * from RequestItem_SubRequestItem x
where x.RequestItem_key = 1 and x.SubRequestItem_key in (
select o.SubRequestItem_key
from SubRequestItem_Entitlement o
inner join SubRequestItem sr on sr.SubRequestItem_key = o.SubRequestItem_key
where o.Entitlement_key = 2 and sr.Action = 'Add' )
And below is my LINQ C# code where I am trying to insert my fixes which include inner join.
z.Entitlements = ARMContext.Context.SubRequestItem_Entitlement
.Where(o => o.Entitlement_key == z.AccessKey && !o.Role_key.HasValue && o.Entitlement.EntitlementConfiguration.UserVisible == true
&& (ARMContext.Context.RequestItem_SubRequestItem
.Where(x => x.RequestItem_key == requestItemKey)
.Select(y => y.SubRequestItem_key)
.Contains(o.SubRequestItem_key)))
.Join(ARMContext.Context.SubRequestItems, subrq => subrq.SubRequestItem_key, temp => requestItemKey, (subrq, temp) => subrq == temp)
Where as previously the C# LINQ code looked like this
z.Entitlements = ARMContext.Context.SubRequestItem_Entitlement
.Where(o => o.Entitlement_key == z.AccessKey && !o.Role_key.HasValue && o.Entitlement.EntitlementConfiguration.UserVisible == true
&& (ARMContext.Context.RequestItem_SubRequestItem
.Where(x => x.RequestItem_key == requestItemKey)
.Select(y => y.SubRequestItem_key)
.Contains(o.SubRequestItem_key)))
When I try to insert the JOIN in the LINQ as per my conditions then I get to see this error.
What is my mistake? Can anybody tell me a correct way to do it?
I think this should Suffice your need, although you might have to make changes to the other code which are dependent on your SubRequestItem_Entitlement table with {user, add}
please have a look at that. As I am sure you will have to make those changes.
.Join(ARMContext.Context.SubRequestItems, user => user.SubRequestItem_key, subreqItems => subreqItems.SubRequestItem_key, (user, subreqItems) => new { user, subreqItems })
.Where(Action => Action.subreqItems.Action == z.ApprovalAction)
you can use this query. I exactly matched the SQL query
var query = ARMContext.Context.RequestItem_SubRequestItem
.Where(a => a.RequestItem_key == 1 && a.RequestItem_key == (ARMContext.Context.SubRequestItem_Entitlement
.Join(ARMContext.Context.SubRequestItems,
right => right.SubRequestItem_key,
left => left.SubRequestItem_key,
(right, left) => new
{
right = right,
left = left
})
.Where(x => x.right.Entitlement_key == 2 && x.left.Action == "Add" && x.right.SubRequestItem_key == a.RequestItem_key).Select(y => y.right.SubRequestItem_key)).FirstOrDefault());

Getting no results from simple Query

I've tried the following and it returned me every Tutor
List<Tutor>tutorsList = tutors.ToList();
Furthermore, I tried to select only Tutors with a specific subject (Tutor-Subject is n:n)
Subject subjectEntity = subjects.Where(s => s.Name == input).FirstOrDefault();
List<Tutor>tutorsList = tutors.Where(t => t.Subjects.Contains(subjectEntity)) .ToList();
As a result, my tutorsList is empty, even subjectEntity is correct (I printed it to console).
But when I loop every Tutor and print the Subjects, there is a Tutor with Subject input.
Any ideas?
If you also have id's, you can do the following:
Subject subjectEntity = subjects
.Where(s => s.Name == input)
.FirstOrDefault();
List<Tutor> tutorsList = tutors
.Where(t => t.Subjects
.Select(x => x.SubjectId)
.Contains(subjectEntity.SubjectId)
)
.ToList();
If not, you can try to do it in a single query
List<Tutor> tutorsList = tutors
.Where(t => t.Subjects.Any(x => x.Name == input))
.ToList()
simplify in one line, using Any, when working on an inner collection.
var tutorsList = tutors.Where(t => t.Subjects
.Any(s => s.Name == input)).ToList();
Try below
Subject subjectEntity = subjects.Where(s => s.Name == input).FirstOrDefault();
List<Tutor>tutorsList = tutors.Where(t => t.Subjects.Any(x=>x.UniqueField==subjectEntity.UniqueField)).ToList();

I want to convert this foreach loop to a LINQ statement

I am not an great at linq by any means but I usually have no issues with a problem of this sort. I want to convert this foreach statement to a LINQ statement:
var existingKeys = new List<int>();
foreach (var taskKey in request.Keys)
{
existingKeys.AddRange(_context.WebTaskGroups
.Where(x => x.TaskGroupNameKey == key && x.TaskKey == taskKey)
.Select(x => x.TaskGroupNameKey));
}
I thought this would do it:
var existingKeys = request.Keys.ForEach(taskKey => _context.WebTaskGroups
.Where(x => x.TaskGroupNameKey == key && x.TaskKey == taskKey)
.Select(x => x.TaskGroupNameKey));
That apparently returns a void not a list...
This:
var existingKeys = request.Keys.Select(taskKey =>
_context.WebTaskGroups
.Where(x => x.TaskGroupNameKey == key && x.TaskKey == taskKey)
.Select(keys => keys.TaskGroupNameKey));
Gives me an "IEnumerable<IQueryable<int>>. So what is the secret sauce that I am missing here?
You shouldn't be performing N database queries in the first place. Using LINQ to perform those N queries instead of a foreach loop doesn't fix that core problem.
You need to re-conceptualize your query so that you have just one query that gets all of the data that you need. In this case that means getting all of the items that match your collection of keys rather than trying to match a single key and then performing N of those queries.
var requestedKeys = request.Keys;
var existingKeys = _context.WebTaskGroups
.Where(x => x.TaskGroupNameKey == key &&
requestedKeys.Contains(x.TaskKey))
.Select(x => x.TaskGroupNameKey))
.ToList();
var existingKeys = request
.SelectMany(r => r.Keys)
.SelectMany(tk =>
_context.WebTaskGroups
.Where(x.TaskGroupNameKey == key && x.TaskKey == tk)
.Select(x => x.TaskGroupNameKey))
.ToList();
var existingKeys = _context.WebTaskGroups
.Where(x => x.TaskGroupNameKey == key && request.Keys.Contains(x.TaskKey))
.Select(x => x.TaskGroupNameKey)
.ToList();
ForEach return a void: http://msdn.microsoft.com/en-us/library/bwabdf9z(v=vs.110).aspx
ForEch: Performs the specified action on each element of the List.
So what to do, is for each item in the list of request.Keys to perform the action to add to the list of existingKeys.
For example:
request.Keys.ForEach(taskKey =>
existingKeys.AddRange(_context.WebTaskGroups
.Where(x => x.TaskGroupNameKey == key && x.TaskKey == taskKey)
.Select(x => x.TaskGroupNameKey));

LinQ method syntax multiple tables with .where() and without joins

How do fetch data from multiple tables with method syntax without using joins, but only .where() methods?
I'm making a select against EF5 db context which maps to this legacy table structure where I have a persons detail table and another table which refers both to itself to create a hierarchy and to the person details table this way:
PersonSet
.Where(p => p.LastName.ToLower()=="surname")
.Join(SubsetSet, p => p.Id, ss => ss.SubsetLink, (p, ss) => new { PersonDetail = p, person = ss })
.Where(m => m.person.Frame=="a" && m.person.Purpose=="1")
.Join(SubsetSet, ss1 => ss1.person.Owner, person => person.SubsetLink, (ss1, ss2) => new { person = ss1, club = ss2 })
.Where(a => a.club.Frame=="b" && a.club.Purpose=="2")
.Join(SubsetSet, ss => ss.club.Owner, ss2 => ss2.SubsetLink, (ss, ss2) => new { club = ss, association = ss2 })
.Where(a => a.association.Frame=="b" && a.association.Purpose=="3")
.Join(SubsetSet, ss => ss.association.Owner, ss3 => ss3.SubsetLink, (ss, ss3) => new { association = ss, district = ss3})
.Where(d => d.district.Frame=="b" && d.district.Purpose=="4" && d.district.SubsetLink=="12345")
.Select(proj => new { proj.association.club.person, proj.association.club, proj.association, proj.district })
.OrderByDescending(a => a.association.club.person.phyperson.FirstName)
.Take(10).Dump();
The above query works at least in LinqPad but, it seems to me that If I could get rid of those joins the statement might look a bit nicer. Now I know, like in the Albahari example below, that this can be done with query syntax. But I couldn't find an example that would illustrate this situation with method syntax. The way I'm trying to approach this might of course be wrong and that's why I can't find suitable examples.
Here I found something similar, but couldn't make it work in LinQPad:
Is multiple .Where() statements in LINQ a performance issue?
Or this one, where the solution is again in query syntax:
Cross Join with Where clause
Or this example by Albahari: (http://www.linqpad.net/WhyLINQBeatsSQL.aspx)
from p in db.Purchases
where p.Customer.Address.State == "WA" || p.Customer == null
where p.PurchaseItems.Sum (pi => pi.SaleAmount) > 1000
select p
Consider this query:
var q = from order in orders
from orderline in order.Lines
where orderline.Count > 10
select order.Discount * orderline.Price;
this more or less corresponds to
var q = orders
.SelectMany(order => order.Lines, (order, orderline) => new { order, orderline})
.Where(item => item.orderline.Count > 10)
.Select(item => item.order.Discount * item.orderline.Price);
For more information on SelectMany, see the MSDN documentation.
If you don't have associations defined:
var q = from order in orders
from orderline in orderLines
where orderline.OrderId == order.Id
where orderline.Count > 10
select order.Discount * orderline.Price;
this more or less corresponds to
var q = orders
.SelectMany(order => orderLines, (order, orderline) => new { order, orderline})
.Where(item => item.orderline.OrderId == item.order.Id)
.Where(item => item.orderline.Count > 10)
.Select(item => item.order.Discount * item.orderline.Price);

Categories