I will use two classes, to demonstrate my examples:
Category LINQ
LINQ Joins
I will use two classes, to demonstrate my examples:
Var and IEnumerable with LINQ
IEnumerable is an interface that can move forward only over a collection, it can’t move backward and between the items. Var is used to declare implicitly typed local variable means it tells the compiler to figure out the type of the variable at compilation time. A var variable must be initialized at the time of declaration. Both have its own importance to query data and data manipulation. Continue reading
How to get the object with the largest value on one of its properties
Let’s assume we have a list of CampaignInfoData objects that have one property named NumberOfBatches of type int.
private List<CampaignInfoData> m_campaignInfosList = new List<CampaignInfoData>(); Continue reading
Find Dictionary duplicate values in C#
Here I use a simple LINQ statement to find duplicate values from dictionary. Continue reading
Handling Nullable values in LINQ queries.
Suppose we have PhoneNum (nullable integer property) in Customer class
Public int PhoneNum {get;set;}
Check Null in lamda expression like below.
Public Ienumerable<Customer> GetCutomers(List<Customer> custList)
{
var query=custList.Where(c => (c.PhoneNum ?? false)== false);
}
LINQ “ThenBy” example.
Explain what the difference is between:
tmp = invoices.InvoiceCollection
.OrderBy(sort1 => sort1.InvoiceOwner.LastName)
.OrderBy(sort2 => sort2.InvoiceOwner.FirstName)
.OrderBy(sort3 => sort3.InvoiceID); Continue reading
Generating sequence of Numbers using Linq.
Public Ienumerable BuildIntegerSequence()
{
var integers=Enumerable.Range(0,10); //0…9
var integers=Enumerable.Range(0,10).Select(i => 5+(10*i)); // 5,15,25…
return integers;
}
Select all items from one collection to another collection of same type
Public class xyz: ICloneable
{
// Properties
public object Clone()
{
return this.MemberwiseClone();
}
}
Null coalescing operator (??) example in C# and using it with LINQ & XML
The ?? operator can be very useful in these scenarios. Continue reading