C# Get Week Number In Year

0 comments

Problem : How do I get number of week per year.

Solution :

// Get Num Of Week Per Year
 public static int Iso8601WeekNumber(DateTime dt)
{
     System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
     int weekNumOnMonth = ci.Calendar.GetWeekOfYear(dt, ci.DateTimeFormat.CalendarWeekRule, ci.DateTimeFormat.FirstDayOfWeek);
     return weekNumOnMonth;
}

Reference:
using System.Globalization;

C# Get Week Number In Month

0 comments

Problem : How do I get number of week per month for the particular date?

Solution :

// Get Num of Week Per Month
public static int GetWeekInMonth(DateTime date)
{
    DateTime tempdate = date.AddDays(-date.Day + 1);
    CultureInfo ciCurr = CultureInfo.CurrentCulture;
    int weekNumStart = ciCurr.Calendar.GetWeekOfYear(tempdate, CalendarWeekRule.FirstFourDayWeek, ciCurr.DateTimeFormat.FirstDayOfWeek);
    int weekNum = ciCurr.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, ciCurr.DateTimeFormat.FirstDayOfWeek);
    return weekNum - weekNumStart + 1;
}

Reference :
using System.Globalization;


For Loop in C# Sample

0 comments



Sample: Code using for loop to pass each of the array variable value.

string[] names = {"Name1","Name2","Name3"};

for (int ArrayNo = 0; ArrayNo < names.Length; ArrayNo++)
{
    string AccountID= names[ArrayNo];
}

How to Retrieve/Get N:N(Many to Many) Records Using C# && fetchXML

0 comments


Problem with retrieving Many to Many records.
Solution: Using C# fetchxml to retrieve all the information. FetchXML is generated through advance find button in CRM 2011.
Advantage: This fetchXML in C# also can be use in one to many (1:N) records too.

Function And Script


#region Fetch N:N Billing Account Filter By Scheduler

string fetchxml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
                                  "<entity name='vw_billingaccountcode'>" +
                                    "<attribute name='vw_billingaccountcodeid' />" +
                                    "<attribute name='vw_name' />" +
                                    "<attribute name='createdon' />" +
                                    "<order attribute='vw_name' descending='false' />" +
                                    "<link-entity name='vw_vw_scheduler_vw_billingaccountcode' from='vw_billingaccountcodeid' to='vw_billingaccountcodeid' visible='false' intersect='true'>" +
                                      "<link-entity name='vw_scheduler' from='vw_schedulerid' to='vw_schedulerid' alias='aa'>" +
                                        "<filter type='and'>" +
                                          "<condition attribute='vw_schedulerid' operator='eq' uiname='Test' uitype='vw_scheduler' value='{" + ID.ToUpper() + "}' />" +
                                        "</filter>" +
                                      "</link-entity>" +
                                    "</link-entity>" +
                                  "</entity>" +
                                "</fetch>";

EntityCollection entities = OrgService.RetrieveMultiple(new FetchExpression(fetchxml));
foreach (Entity result in entities.Entities)
{
       string BillingAccountId = result.Id.ToString(); //All the result will be show here. Looping.
}

#endregion