user5740086 user5740086 - 7 months ago 12
SQL Question

how should I use order by on distinct in entities

I have this code

var chats = (from c in _entities.Chats
where c.SenderId == userId || c.ReceiverId == userId

select new ChatList()
{
Logo = c.SenderId != userId ? c.Company.LogoUrl:c.Company1.LogoUrl,
Name = c.SenderId != userId? c.Company.CompanyPersonName:c.Company1.CompanyPersonName,
UserId = c.SenderId!=userId ?c.Company.CompanyId : c.Company1.CompanyId
}
).Distinct();


how should I add , "order by" like this :

ORDER BY c.SenderId

Answer

Add OrderByat the end of it:

   var chats = (from c in _entities.Chats
    where c.SenderId == userId || c.ReceiverId == userId

        select new ChatList()
         {
          Logo = c.SenderId != userId ? c.Company.LogoUrl:c.Company1.LogoUrl,
          Name = c.SenderId != userId? c.Company.CompanyPersonName:c.Company1.CompanyPersonName,
        UserId = c.SenderId!=userId ?c.Company.CompanyId : c.Company1.CompanyId 
        }
    ).Distinct().OrderBy(x => x.UserId)
Comments