0

I have a SQL to view table master and detail. How convert to linq. Please help me :

Select B.idchatuser,B.Nama, B.Hp,
(select TOP 1 CreatedTime from ChatUserConversation 
where IdChatUser = B.IdChatUser order by CreatedTime) CreatedTime
from ChatUser B 
order by CreatedTime desc
3
  • Have you tried Linqer sqltolinq.com ? Commented Jun 20, 2016 at 2:14
  • Please make an attempt, Arly. We are not a code-writing service. The query is relatively simple, you should be able to make a start. Commented Jun 20, 2016 at 2:14
  • @Kason : Thanks for the solution, i have downloaded and tried Linqer. But I do not understand how to use Commented Jun 20, 2016 at 2:40

2 Answers 2

2

How about this?

from B in ChatUser
join C in ChatUserConversation on B.IdChatUser equals C.IdChatUser
group new {B,C} by new {B.IdChatUser, B.Nama, B.Hp} into g
let CreatedTime = g.OrderBy(x=>x.C.CreatedTime).Select(x=>x.C.CreatedTime).FirstOrDefault()
orderby CreatedTime descending
select new 
{
    IdChatUser = g.Key.IdChatUser, 
    Nama = g.Key.Nama, 
    Hp = g.Key.Hp, 
    CreatedTime = CreatedTime
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you,,, this work but the results are not ordered by CreatedTime descending
1

Assuming you have ChatUser and ChatUserConversation C# entities, you could do this.

var results = ChatUser.Select(x=> new 
         {
             x.idchatuser,
             x.Name,
             x.Hp,
             CreatedTime =  ChatUserConversation .Where(c=> c.IdChatUser = x.IdChatUser)
                                                 .Max(c=>c.CreatedTime)  
         })
        .ToList(); 

3 Comments

i have tried this solution, but don't know how parsing the results to view. i use asp.net mvc C#
You mean query is working, but don't know how to use it in view?
i don't know working or not because when i debug i get error in view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.