Friday, January 21, 2011

LINQ Left Left Outer Join

I wrote an application for my company which required me to use data coming from an external application (Microsoft Dynamics AX). This is actually a view that is generated by a job every night.

The table contains all the branches information (Name, Address, Country, ID, ect).

When the user goes to the main form of this application, he/she sees a GridView with all the branches' tasks. When he/she clicks edit, a Drop Down List is bind to the Branches table where the group of the user is equal to the group assigned in the Branches Table.

For Example:
var GridBind =
          from x in MainQueryjoin cb_s in db.CommentBase_Status on   
          x.CommentStatusID equals cb_s.StatusIDjoin b in db.Branches on
          x.BranchID equals b.dimensionorderby x.EntryDate ascending


where x.CommentType == CommentType && x.CommentStatusID == CommentValue
select new
{
FormId = x.FormID,
branch_group = x.BranchGroupID,
BranchId = branches.BRANCHNAME,
CommentString = x.CommentString,
EntryDate = x.EntryDate
};


The problem was that I needed to create a brach name = All with a value of 1, and we weren't able to add this to the Branches table as it would affect other queries that the analysis server is using.

My application Data layer is implemented with LINQ, so I decided to add these values when the Drop Down List was being bound, for example:

Well, then to do  Let Outer Join in the main query of my application I can do the following:


var GridBind =
                from x in MainQuery
                join cb_s in db.CommentBase_Status on x.CommentStatusID equals cb_s.StatusID
                orderby x.EntryDate ascending
                where x.CommentType == CommentType && x.CommentStatusID == CommentValue
                from branches in db.Branches.Where(b => b.dimension == x.BranchID).DefaultIfEmpty()
                select new
                {
                    FormId = x.FormID,
                    branch_group = x.BranchGroupID,
                    BranchId = (branches.BRANCHNAME == null)? "All" :branches.BRANCHNAME,
                    StatusName = cb_s.StatusName,
                    CommentString = x.CommentString,
                    EntryDate = x.EntryDate
                };
            try
            {
                GridObject.DataSource = GridBind;
                GridObject.DataBind();
                ret = true;
            }
            catch (Exception ex)
            {
                ex.ToString();
                ret = false;
            }

List<Branches> branches = new List<Branches>();
                var ddlBranchesList =
                    from br in db.Branches
                    where br.active == true && br.BRANCHGROUP == id
                    orderby br.dimension descending
                    select br;

                branches = ddlBranchesList.ToList<Branches>();
                branches.Add(new Branches() { BRANCHNAME = "All", dimension = 1 });
                branches.Add(new Branches() { BRANCHNAME = "Select One", dimension = 0 });
                branches.Reverse();
                ddl.DataSource = branches;
                ddl.DataTextField = "BRANCHNAME";
                ddl.DataValueField = "dimension";

1 comment:

  1. The format is a little weird. If you have any questions about the code contact me.

    ReplyDelete

Thank you for your thoughts. Your comment will appear in my blog shortly after review.

Have a great day!