Thursday, November 17, 2011

Left Outer Join of Two DataTable Using Linq


This method will take two table as input. This function will provide Left Outer join between two table and also performs any kind of math operation needed on the table. For example : along with the left outer join we may want to add columns of two table to produce different column value. In below example two columns from two different table are added . After the Left Outer join, Left Table will contain added value .

public static DataTable LeftOuterJoinTables(DataTable LeftTable, DataTable RightTable)
        {
            DataTable dtJoinedTable = null;
            var resultQuery = from TableA in LeftTable.AsEnumerable()
                                join TableB in RightTable.AsEnumerable()
                                on new
                                {
                                    ColumnA = TableA.Field(LeftTable.Columns[0]),
                                    ColumnB = TableA.Field(LeftTable.Columns[1])
                                }
                                equals new
                                {
                                    ColumnA = TableB.Field(RightTable.Columns[0]),
                                    ColumnB = TableB.Field(RightTable.Columns[1])
                                }
                                into GJ
                                from sub in GJ.DefaultIfEmpty()
                                select new
                                {
                                    Column1 = TableA.Field(LeftTable.Columns[0]),
                                    Column2 = TableA.Field(LeftTable.Columns[1]),
                                    Column3 = sub == null ? TableA.Field(LeftTable.Columns["Variable Column"]) :
                                              sub.Table.TableName.ToUpper().Contains("Operation to perform") ? Convert.ToString
                                              (Convert.ToDecimal(TableA.Field(LeftTable.Columns["Column Conataining Values"])) +
                                              Convert.ToDecimal(sub.Field(RightTable.Columns["Column Conataining Values"]))) : ""
                                };
                       dtJoinedTable = resultQuery.ExtCopyToDataTable();


            return dtJoinedTable;
        }

Programatically Check if currently logged in User belongs to Given Group


public virtual bool IsCurrentUserIsGroupMember(string groupName)
        {          
            bool isGroupMember = false;          
            SPWeb web = SPContext.Current.Web;
            string currentLoggeInUser = web.CurrentUser.LoginName;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(web.Site.ID))
                {
                    using (SPWeb currentWeb = site.AllWebs[web.ServerRelativeUrl])
                    {
                        currentWeb.AllowUnsafeUpdates = true;
                        foreach (SPGroup group in currentWeb.SiteGroups)
                        {
                            if (group.Name == groupName)
                            {
                                foreach (SPUser user in group.Users)
                                {
                                    if (user.LoginName.ToLower() == currentLoggeInUser.ToLower())
                                    {
                                        isGroupMember = true;
                                    }
                                }
                            }
                        }
                    }
                }
            });        
           
            return isGroupMember;
        }

Programatically obtain Email Address of Share point user (SPUser) From Display Name using Linq

This method will take the given  Display Name and looks for associated  email address on User Information List:
               
                    string  strDisplayName = "John Doe";
                    string  strEmailAddress =string.Empty;
                    SPList userInfoList = item.File.Web.SiteUserInfoList;
                    IEnumerable userInfoItem = userInfoList.Items.OfType();
                    var qryEmailAddress = from row in userInfoItem
                                          where Convert.ToString(row["ImnName"]) == strDisplayName
                                          select row;
                    if (qryEmailAddress.Count() > 0)
                    {
                        strEmailAddress = Convert.ToString(qryEmailAddress.First()["EMail"]);
                    }