Home > Linq > LINQ # 3 – LINQ To Collections (List,Arraylist,String,TextFile,Array)

LINQ # 3 – LINQ To Collections (List,Arraylist,String,TextFile,Array)

Hi folks,

Mean while i was so busy with work so didn’t able to post last week.

Today we will see

 LINQ to Collections

1) LINQ To List

suppose we have one class named IceCream

public class Icecream
public string Name { get; set; }
public string Ingredients { get; set; }
public string TotalFat { get; set; }
public string Cholesterol { get; set; }
public string TotalCarbohydrates { get; set; }
public string Protein { get; set; }
public double Price { get; set; }


Now build the Icecreams list collection using the class defined perviously.

List<Icecream> icecreamsList = new List<Icecream>
new Icecream {Name="Chocolate Fudge Icecream", Ingredients="cream,
milk, mono and diglycerides…"
, Cholesterol="50mg",
Protein="4g", TotalCarbohydrates="35g", TotalFat="20g",
new Icecream {Name="Vanilla Icecream", Ingredients="vanilla extract,
guar gum, cream…"
, Cholesterol="65mg", Protein="4g",
TotalCarbohydrates="26g", TotalFat="16g", Price=9.80 },
new Icecream {Name="Banana Split Icecream", Ingredients="Banana, guar
gum, cream…"
, Cholesterol="58mg", Protein="6g",
TotalCarbohydrates="24g", TotalFat="13g", Price=7.5 }


Suppose from above list which is containing three objects we want the Icecream with less price,for this we can use looping technique where we need to look at the value of each icecream object which is quite not acceptable.

Using LINQ we can avoid this by just firing the query on the list collection.

For EX. We want to know the icecream which prices less than 10 ,in that case we can write the LINQ query as follows:-

IEnumerable<Icecream> IcecreamsWithLessPrice =
from ice in Icecreams
where ice.Price < 10
select ice;

2) LINQ To ArrayList

ArrayList arrListIcecreams = new ArrayList();
arrListIcecreams.Add( new Icecream {Name="Chocolate Fudge Icecream",
Ingredients="cream, milk, mono and diglycerides…",
Cholesterol="50mg", Protein="4g", TotalCarbohydrates="35g",
TotalFat="20g", Price=10.5 });
arrListIcecreams.Add( new Icecream {Name="Vanilla Icecream",
Ingredients="vanilla extract, guar gum, cream…",
Cholesterol="65mg", Protein="4g", TotalCarbohydrates="26g",
TotalFat="16g", Price=9.80 });
arrListIcecreams.Add( new Icecream {Name="Banana Split Icecream",
Ingredients="Banana, guar gum, cream…", Cholesterol="58mg",
Protein="6g", TotalCarbohydrates="24g", TotalFat="13g", Price=7.5


Following is the query to fetch low priced ice-creams from the list.

var queryIcecreanList = from Icecream icecream in arrListIcecreams
where icecream.Price < 10
select icecream;


3) LINQ To String

As we know that strings are also collections of characters so its quite easy to play with the characters in a string using LINQ

  string aString = "Ganesh Divekar";

            IEnumerable<char> result = from chr in
where char.IsUpper(chr)
select chr;

//It will give o/p as G,D
3) LINQ To Text  Files
As Text Files are also a kind of collection we can apply LINQ to it also.
List<string> IcecreamNames = new List<string>();
using( StreamReader sReader = new StreamReader(@"C:\Icecreams.txt"))
string str;
str = sReader.ReadLine();
while (str != null)
The following sample code reads the list of strings and retrieves the name of
ice-creams in descending order:
IEnumerable<string> icecreamQuery =
from name in IcecreamNames
orderby name descending
select name;


I hope this will help you more in understanding LINQ in Detail


Categories: Linq
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: