Monday, June 22, 2015

Find Gridview Row From List and check it.

foreach (var item in listPeople)
{
 var row = gvAddPeople.Rows.Cast<GridViewRow>()
.Where(x => ((HiddenField)x.Cells[0].Controls[3]).Value == item.PeopleID.ToString()).Single();

 ((CheckBox)row.Cells[0].Controls[1]).Checked = true;
}

Monday, June 1, 2015

Advanced LINQ

Get same attribute between 2 List

var listPerson = AlllistPerson.Where(x => listPhone
 .Any(y => x.PersonPhone.Contains(y.PhoneNmbers).ToList();

Remove from another List (duplicate)

var result = personList.Where(c => !transactionList.Exists(b => c.phone == b.phone)).ToList();

Remove from another List (duplicate) & Child List


List<long> phoneNumbers = sessionTelco.SelectMany
                            (x => x.PhoneList.Select(y => y.phone)).ToList();

coveragePrices = coveragePrices.Where(x => ! phoneNumbers.Contains(x. phone)).ToList();

Selecting First Item from every group

var grouped = personList.GroupBy(x => x.CountryName).Select(y => y.First()).ToList();

 var grouped = from x in personList
            group x by x.CountryName
            into y
            select y.First();


select many from child list.


List<long> routeClassIDList = _upc.SessionClientGroupList.SelectMany
         (x => x.ClientGroupCoverageList.Select(y => y.RouteClassID)).ToList();


filter all child list same with list


clientGroups = clientGroups.Where(x => !x.ClientGroupCoverageList.All(y => routeClassIDList.Contains(y.RouteClassID))).ToList();


ForEach Add if else


people.ForEach(x => { if (x.Balance.HasValue == false || x.Balance.Value == 0) x.Balance = null; });


INNER JOIN 
devicePerAccList = (from f in devicePerAccList
                    join s in Accounts on f.AccountId equals s.E2AccountId
                    select new DevicePerAcc
                    {
                        AccountId = f.AccountId,
                        AccCode = s.AccountCode,
                        NoOfDevice = f.NoOfDevice

                    }).ToList();

Select Many, Find Description, Filter Description = null, Order By 1A , 2B , 11A

preBookMealList = ssrReportList.SelectMany(a => a.SSRCodes.Select(b => new PreBookMeal
{
SSRCode = b,
SSRDescription = ssrMealList.Where(c => c.SSRCode == b).Select(d => d.Name).FirstOrDefault()
})
.Where(e => e.SSRDescription != null))
.OrderBy(f => int.Parse(f.SeatNo.Remove(f.SeatNo.Length - 1)))
.ToList();


Dictionary put Key to object ID

result = peopleList.Select(x => { x.Value.peopleID = x.Key; return x.Value; }).ToList();

Saturday, April 11, 2015

Bootstrap

HTML - CSS - JavaScript
Make sure to add jQuery file above the bootstrap file.

<div class="container-fluid">
Full width container

<div class="container">
Fixed width container

btn Modifier
–Default
–Primary
–Success
–Info
–Warning
–Danger

Saturday, April 4, 2015

Parse XML File


  1. Reference System.XML
  2. Use XmlReader to parse the text string
  3. Use XmlReader to read the xml data

string xmlString =
                @"<bookstore>
                    <book genre='autobox' publishDate='2015-03-04'>
                        <title>Many bottle</title>
                        <author>
                            <firstName>David</firstName>
                            <lastName>Baby<lastName>
                        </author>
                    </book>
                  </bookstore>";

StringBuilder result = new StringBuilder();

using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
   reader.ReadToFollowing("book");
   reader.MoveToFirstAttribute();
   string genre = reader.Value;
   result.AppendLine("Genre value :" + genre);

   reader.ReadToFollowing("title");
   result.AppendLine("title element :" + reader.ReadElementContentAsString());
}

Console.Write(result);



Saturday, March 21, 2015

C# Common Question

1) String vs string
string for creating variable
//string name = "Sam"

String for class methods and reference
// String.Concat(name, name2)

2) Count Occurrences of a String
Use Regex.Matches(), Not use Loop

3) Break vs Continue
Break - Exit Immediately
Continue - allow loop to continue (Stop the current and continue next )

5)
String can be null (reference type)
Datetime not null (value type )

6)
double x = 5.0;
int y =5;
console.writeline(x==y);

7) Ref vs Out

ref tells the compiler that the object is initialized before entering the function.
out tells the compiler that the object will be initialized inside the function.
ref is two-ways
out is out-only. (1 way )
out not allowed to leave it unassigned.


int a, b; 
swap(ref a, ref b); Compile Error (ref)

int a = 0;
int b = 0;

swap(ref a, ref b); Right (ref)

int a, b; 
swapOut(out a, out b); Right (out)

static void swapOut(out int o, out int p)
{
    int x = 0;
     x = o; Error(Out is not allow to assign);
     x = 1000;
     p = 2000;

}

10)Inheritance

If Parent have constructor, child also must have constructor

public Rectangle(double l, double w) //Parent
{
      length = l;
      width = w;

}

public Tabletop(double l, double w) : base(l, w) //Child 
{
}

10)Const Vs Readonly
const must be initialized, initialization must be at compile time
readonly can use default value, without initializing. initialization can be at run time

11)Convert.tostring vs .tostring()
Convert.tostring handle null.

Tostring() not handle null.


Sunday, March 15, 2015

Update & Delete Statement With Inner join

UPDATE Table1 SET Column1=@Value
FROM Table1 a
INNER JOIN Table2 b ON a.ID = b.ID
WHERE b.ID=1 

-----------------------------------

DELETE Table1
FROM Table1 a
INNER JOIN Table2 b on a.ID = b.ID
WHERE b.ID is not null

------------------------------------

Delete Top Number in Table

DELETE TOP (8) FROM Table1
WHERE ID = 1

Thursday, March 5, 2015

Extract From Excel File

// Row and Column Count is Start From 1  

private
List<long> SelectDefaultRouteClassByExcel()
{
    List<long> result = new List<long>();

    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    Excel.Range range;

    int rowCnt = 0;

xlApp = new Excel.Application();

xlWorkBook = xlApp.Workbooks.Open("D:\\Load\\Route\\excelList.xlsx", 0, true, 5, "", "",      true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true,      1, 0);

    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    range = xlWorkSheet.UsedRange;

    for (rowCnt = 2; rowCnt <= range.Rows.Count; rowCnt++)
    {
        var hasObject = ((range.Cells[rowCnt, 3] as Excel.Range).Value2);

        if (hasObject != null)
        {
           long item = range.Cells[rowCnt, 3] as Excel.Range).Value2);
           result.Add(item);
         }
     }

       xlWorkBook.Close(true, null, null);
       xlApp.Quit();

       releaseObject(xlWorkSheet);
       releaseObject(xlWorkBook);
       releaseObject(xlApp);

       return result;
}


private void releaseObject(object obj)
{
Try
{
       System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
     }
     catch (Exception ex)
     {
         obj = null;
         MessageBox.Show("Unable to release the Object " + ex.ToString());
      }
      finally
      {
          GC.Collect();
       }
}
----------------------------------

Date Time = DateTime.FromOADate(((range.Cells[rowCnt, 3] as Range).Value2))