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

Thursday, February 18, 2010

Get URL Parameters using LINQ

Dictionary result = new Dictionary();

String urlString = "http://www.jwize.com?param1=valu1¶m2=value2";

var query = from match in urlString.Split('?').Where(m => m.Contains('='))
.SelectMany(pr => pr.Split('&'))
where match.Contains('=')
select new KeyValuePair(
match.Split('=')[0],
match.Split('=')[1]);
query.ToList().ForEach(kvp => result.Add(kvp.Key, kvp.Value));

LINQ Conditional Count

MyList.Count(x => x.Product == "Apple")

LINQ Conditional Sum

MyList.Where(x => x.Product == "Apple").Sum(x => x.TotalSales)

evaluation of LINQ expression using .ToArray()

IEnumerable windowsFiles = System.IO.Directory.GetFiles(Environment.GetEnvironmentVariable("SystemRoot"), "*.INF", System.IO.SearchOption.AllDirectories);

IEnumerable files =
(
from f in windowsFiles
where System.IO.File.ReadAllText(f).Contains(searchText)
select System.IO.Path.GetFileNameWithoutExtension(f)
).ToArray(); // ToArray forces immediate evaluation.

Linq2Sql - Group By Count

public List GetTotalRegisteredUserCount()
{
var qry = from aspnet_Profile profile in aspnet_Profiles
group profile by profile.Country into grp
select new {Country = grp.Key,
Count = grp.Select(x => x.Country).Distinct().Count()
};

return qry.ToList();
}

Simple XML parsing using LINQ




The Reddest

Creating an XP Style WPF Button with Silverlight

2/20/2008


The Fattest

Flex And Yahoo Maps

2/12/2007


The Tallest

WPF Tutorial - Creating A Custom Panel Control

2/18/2008



XDocument xmlDoc = XDocument.Load("TestFile.xml");

var tutorials = from tutorial in xmlDoc.Descendants("Tutorial")
select new {
Author = tutorial.Element("Author").Value,
Title = tutorial.Element("Ttle").Value,
Date = tutorial.Element("Date").Value };