Pages

Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Thursday, October 3, 2013

Random Aplhanumeric String Generator- C#

This function Returns Random Generate string ...

 private string RandomGenerator()
    {
        var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        var random = new Random();
        var result = new string(
            Enumerable.Repeat(chars, 8)
                      .Select(s => s[random.Next(s.Length)])
                      .ToArray());
        return result.ToString();
    }




Call This function:

Page_load(eve..)
{
string UserID=RandomGenerator( );
}

Tuesday, September 24, 2013

Copy unique rows of a column from one Datatable to other Datatable - LINQ



Here , We have to copy the unique rows of column 'CategoryName' from DataTable 'dt_old' to the 'Category' column of DataTable 'dt_Categories' using LINQ.


 DataTable dt_Categories = new DataTable();
 dt_Categories.Columns.Add("Category", Type.GetType("System.String"));

DataTable dt_old = new DataTable();
dt_old = (DataTable)Session["dt"];


 dt_Categories = dt_old.AsEnumerable().Select(row =>
            {
                DataRow newRow = dt_Categories.NewRow();
                newRow["Category"] = row.Field<string>("CategoryName");
                return newRow;
            }).Distinct(DataRowComparer.Default).CopyToDataTable();






Monday, September 2, 2013

Create XML through LINQ - C#


1. Firstly, Add namespace to use "XElement " class: 

 using System.Xml.Linq;


2. Call this Method to get the xml in string "
getXML" :

 public void MethodToWriteXml()
    {     

XElement contacts =
    new XElement("Contacts",
        new XElement("Contact",
            new XElement("Name", "Patrick Hines"),
            new XElement("Phone", "206-555-0144", 
                new XAttribute("Type", "Home")),
            new XElement("phone", "425-555-0145",
                new XAttribute("Type", "Work")),
            new XElement("Address",
                new XElement("Street1", "123 Main St"),
                new XElement("City", "Mercer Island"),
                new XElement("State", "WA"),
                new XElement("Postal", "68042")
            )
        )
    );
        string getXML = contacts.ToString();
    }




Output: (get output in string 'getXML')

<Contacts>
  <Contact>
    <Name>Patrick Hines</Name>
    <Phone Type="Home">206-555-0144</Phone>
    <phone Type="Work">425-555-0145</phone>
    <Address>
      <Street1>123 Main St</Street1>
      <City>Mercer Island</City>
      <State>WA</State>
      <Postal>68042</Postal>
    </Address>
  </Contact>
</Contacts>

Thursday, July 11, 2013

Remove the Duplicate Rows from Data Table using LINQ - C#

"dt" is the datatable with the duplicate rows , now we have to remove the duplicate rows 

the field on behalf on which we are removing the duplicate rows is the "Name" field.

var s = dt.AsEnumerable().GroupBy(t => t[0]).Where(t1 => t1.Count() > 1).ToList();


// make new datatable same as the dt, which will contains the unique rows

         DataTable dt_new = dt.Clone();   

// get rows from var "s" and insert into the new datatable "dt_new"

        foreach (var item in s)
        {
            DataRow dr =
dt_new.NewRow();
            dr["Name"] = item.Key.ToString();
           
dt_new.Rows.Add(dr);
        }


"dt_new" is the datatable contains all unique rows acc. to the Name

Filter DataTable using LINQ - C#


Here we have a DataTable "dt" with the various fileds with Duplicate data.

For Example we have records for population Country wise in the dt.

If, we have to show data acc. to "INDIA"

 DataTable dt_filtered = dt.AsEnumerable()  .Where(a => Convert.ToString(a["Country"]) = = "INDIA") .CopyToDataTable();

 Here "Country" is the field in the datatable by with we are filtering

"dt_filtered" contains data INDIA wise.

--------------------
If, we have to show data acc. to "US", Then 

DataTable dt_filtered = dt.AsEnumerable()  .Where(a => Convert.ToString(a["Country"]) = = "US") .CopyToDataTable();

"dt_filtered" contains data US wise.