Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Answers


You can perform Inner join, outer joins and some custom joins with the help of LINQ to SQL.

Following example returns leave types by added user details using LINQ to SQL inner joins.

public List<LeaveType> GetAllLeavTypes()
        {
 
            
List<LeaveType> Types = new List<LeaveType>();
            
using (ApplicationServices BlogDbContext = newApplicationServices())
            {
                
var LeaveTypes = (from l in BlogDbContext.LeaveType
                                  
join e in BlogDbContext.User
                                  
on new { PID = l.AddedBy } equals new { PID = e.UserId }
                                  
select new
                                  {
                                      TypeId = l.TypeId,
                                      LeaveTitle = l.LeaveTitle,
                                      Description = l.Description,
                                      AddedBy = l.AddedBy,
                                      UpdateBy = l.UpdateBy,
                                      CreatedDate = l.CreatedDate,
                                      UpdateDate = l.UpdateDate,
                                      CreatedBy = e.UserName
                                  }
 
                     ).ToList();
                
foreach (var l in LeaveTypes)
                {
                    
LeaveType type = new LeaveType();
                    type.CreatedBy = 
new aspnet_Users();
                    type.TypeId = l.TypeId;
                    type.AddedBy = l.AddedBy;
                    type.LeaveTitle = l.LeaveTitle;
                    type.Description = l.Description;
                    type.CreatedDate = l.CreatedDate;
                    type.CreatedBy.UserName = l.CreatedBy;
                    type.UpdateBy = l.UpdateBy;
                    type.UpdateDate = l.UpdateDate;
                    Types.Add(type);
                }
 
            }
            
return Types;
        }

Share your answer


Share as a guest

Please subscribe me to the CodeChef4U.

By creating your new answer, you agree to the privacy policy and terms of service.