question

NAUSHADALI-0781 avatar image
0 Votes"
NAUSHADALI-0781 asked BrandoZhang-MSFT edited

How to convert sql into linq

select AdminUsers.AdminUserID ,AdminUsers.FirstName,AdminUsers.LastName, AdminUsers.City, AdminUsers.ContactNumber, AdminUsers.Address, AdminUsers.Email,
AdminUsers.Gender, AdminUsers.ZipCode, BloodGroupMater.BloodGroupName, CountryMaster.CountryName, Role.RoleName, StateMaster.StateName from AdminUsers
inner join BloodGroupMater ON AdminUsers.BloodGroupID = BloodGroupMater.BloodGroupID
inner join CountryMaster ON AdminUsers.CountryID = CountryMaster.CountryID
inner join Role ON AdminUsers.RoleID = Role.RoleID
inner join StateMaster ON AdminUsers.StateID=StateMaster.StateID
where AdminUserID=1;

dotnet-aspnet-core-blazor
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

BrandoZhang-MSFT avatar image
0 Votes"
BrandoZhang-MSFT answered BrandoZhang-MSFT edited

Hi @NAUSHADALI-0781,

Since we don't know how you entity model makeup , we could't directly provide a workable linq to you. But we could tell you how you could build it. Something like below:

      var data = (from u in db.AdminUsers join up in db.BloodGroupMater on u.BloodGroupID equals up.BloodGroupID
                              join p in db.CountryMaster on  u.CountryID equals p.CountryID
                              join r in db.Role on u.RoleID equals r.RoleID  
                              join s in db.StateMaster on u.StateID equals s.StateID
 `                            where u.AdminUserID == 1 
                              select new 
                              {
                                  AdminUserID = u.AdminUserID,
                                  FirstName= u.FirstName,
                                 LastName=u.LastName,
                                 City=u.City,
                                ContactNumber=u.ContactNumber,
                                 Address=u.Address,
                                Email=u.Email,
                               Gender = u.Gender,
                             ZipCode=u.ZipCode,
                            BloodGroupName = up.BloodGroupName,
                            CountryName = p.CountryName,
                              RoleName =r.RoleName ,
                             StateName  = s.StateName
                              }).Distinct().ToList(); 






5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.