Showing posts with label Linq. Show all posts
Showing posts with label Linq. Show all posts

Wednesday, May 18, 2011

If you want to return a query with Default if its Empty?

If you want to return a query with default value if it is empty you can go with DefaultIfEmpty

void Main()
{
Coder coder = new Coder("Jon");

List coderCollection = new List()
{
new Coder("Anish"),
new Coder("marokey"),
};

var result = coderCollection.Where(r => r.Name == "Pat").DefaultIfEmpty(coder);

}

// Define other methods and classes here
class Coder
{
public string Name;

public Coder(string Name)
{
this.Name = Name;
}
}

Order by with out Culture

If you want to do some operations without any culture you can do with the help of StringCompare.Ordinal or StringCompare.OrdinalIgnoreCase.

void Main()
{
string[] nonCulture = { "äax", "ääü", "äbü" };

IEnumerable orderNonCulture = nonCulture.OrderBy( r => r, StringComparer.Ordinal);

orderNonCulture.Dump();
}

Sunday, May 15, 2011

?? Operator used in Linq

Here is the Msdn link for ?? Operator

if need to order by a query which need to orderby column1 if not null, if null orderby column2

void Main()
{

List orderList= new List()
{
new Order(1, 4),
new Order(null,6),
new Order(2,5),
new Order(null,3),
};

var k = from p in
orderList
orderby p.ID ?? p.Count
select p;
}


class Order
{
public int? ID{get;set;}
public int? Count{get;set;}


public Order(int? id,int? count)
{
ID =id;
Count =count;
}
}

A simple example to say the difference between Descendants vs Elements

Here is the XML









if use Descendants

XDocument xDoc = XDocument.Load(@"C:\Users\anishmarokey\Desktop\XMLFile1.xml");

var result = from book in xDoc.Descendants("Book")
select new
{
Title = book.Attribute("Title"),
Author = book.Attribute("Author")
};

if use Elements

XDocument xDoc = XDocument.Load(@"C:\Users\anishmarokey\Desktop\XMLFile1.xml");

var result = from book in xDoc.Elements("Titles").Elements("Book")
select new
{
Title = book.Attribute("Title"),
Author = book.Attribute("Author")
};

Sort a class Employee

Here is a simple example for sorting an Employee class

public class MainClassToSortEmployee
{
public enum SortDirection { Ascending, Decending }

public static List Sort1(ref List list,
Func sorter, SortDirection direction)
{
if (direction == SortDirection.Ascending)
return list = list.OrderBy(sorter).ToList();
else
return list = list.OrderByDescending(sorter).ToList();
}

static void Main()
{

}
}

public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime dateTime { get; set; }

public Employee(string p, string p_2, DateTime dateTime)
{
this.FirstName = p;
this.LastName = p_2;
this.dateTime = dateTime;
}
}

next task is to convert it to a Generic Linq

Wednesday, May 11, 2011

Concat vs Union in C#

List list1 = new List(){1,2,3};
List list2 = new List(){1,2,4};

var dummyConcat = list1.Concat(list2); // combine all values without distinct
var dummyUnion = list1.Union(list2);// combine all values with distinct

result for dummyConcat




result for dummyUnion




Why Field is required for DataTable Linq

Here is one good blog for the same Click here

E.g

DataTable dt = new DataTable();

//Adding Columns and names
dt.Columns.Add("ID" ,typeof(int));
dt.Columns.Add("Name",typeof(string));

//Adding Rows and values

dt.Rows.Add(1,"anish");
dt.Rows.Add(2,"marokey");
dt.Rows.Add(2,"varghese");

//querying the result where ID is 2
var result = from p in dt.AsEnumerable()
where p.Field("ID") == 2
select p.Field("Name");

Thursday, May 5, 2011

EnumerateDirectories in .Net 4.0

if you want to return Enumerable collection of file information you can use EnumerateFiles

DirectoryInfo DirInfo = new DirectoryInfo(@"\\dir");

IEnumerable = DirInfo.EnumerateFiles();

Here is the link for Files,Directories,File system information and Lines from a text file

MsdnLink: http://msdn.microsoft.com/en-us/library/dd997370.aspx