Hukam
Hukam

Reputation: 1026

LINQ join in Best way without multiple from clause

I am new to LINQ. It's my Linq Query Here

var users = from UserTbl in entity.User
            from grpTbl in entity.Group
            from role in entity.Role
            where grpTbl.groupID == UserTbl.groupID&& UserTbl.userID==role.userID 
            select new Contract.User()
            {
                UserId = UserTbl.userID,
                UserName = UserTbl.userName,
                FirstName = UserTbl.firstName,
                LastName = UserTbl.lastName,
                GroupId = grpTbl.groupID,
                GroupName =grpTbl.groupName,
                DesignationID = role.roleID    
            };

How can I write this query in the best way?

Upvotes: 0

Views: 197

Answers (1)

Andrey
Andrey

Reputation: 60065

it is pretty fine. but I prefer joins because they make possible to remove "matching" expressions out of where clause, so that only "true" filtering remains there.

var users = from UserTbl in entity.User
            join grpTbl in entity.Group on grpTbl.groupID equals groupId            
            join role in entity.Role on UserTbl.userID equals role.userID 
            select new Contract.User()
            {
                UserId = UserTbl.userID,
                UserName = UserTbl.userName,
                FirstName = UserTbl.firstName,
                LastName = UserTbl.lastName,
                GroupId = grpTbl.groupID,
                GroupName =grpTbl.groupName,
                DesignationID = role.roleID    
            };

Upvotes: 3

Related Questions