Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
this is the query
when i wrote it in Linq an error appear

select u.user_name ,max(l.last_action_date)
from Users u join system_log l on u.user_id = l.user_id
group by u.user_name


What I have tried:

the error in some case is
The specified LINQ expression contains references to queries that are associated with different contexts.
Posted
Updated 29-Sep-19 18:49pm
Comments
F-ES Sitecore 1-May-19 5:45am    
You'll probably need to post the code you are using as that error doesn't sound like it is anything to do with your linq code.

Here's how you do max:

c# - Using Linq to SQL, how do I find min and max of a column in a table? - Stack Overflow[^]

Here is how you group by:

var totalProdcuts = from o in ctx.Orders
                    join p in ctx.Products on o.ProductId equals p.Id
                    group o.NumberOf by p.Name into g
                    select new { ProductName = g.Key, TotalOrdered = g.Sum() };
 
Share this answer
 
Comments
KhaledMohammad 1-May-19 6:08am    
i can do it separately
but i need them together
Christian Graus 1-May-19 6:11am    
The example has a sum. Replace it with Max.
KhaledMohammad 1-May-19 6:43am    
The problem was to use two different dbcontext in the same linq
Now the problem has been solved by putting them in a list before doing linq
Thx
Christian Graus 1-May-19 16:54pm    
yeah, that's what I would have said if the question had been clearer :)
Hi
Your SQL query will look like below in Linq:

var query = from u in Users
join i in system_log on
u.user_id equals i.user_id
group u by u.user_name into usernew
select new 
{
  username = usernew.Key
  last_action_date = g.Max( p => p.usernew.last_action_date)
}

Hope This will help you.
Thank you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900