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

Thursday, May 24, 2012

LINQPad

string[] names = {"Michael", "Long"};

IEnumerable<string> query = names
.Where(n=>n.Contains("o"))
.OrderBy(n=>n.Length)
.Select(n=>n.ToUpper());

query.Dump();

// same query as above
IEnumerable<string>filtered = names.Where(n=>n.Contains("o"));
IEnumerable<string>sorted = filtered.OrderBy(n=>n.Length);
IEnumerable<string>finalQuery = sorted.Select(n=>n.ToUpper());

filtered.Dump("Filtered");
sorted.Dump("Sorted");
finalQuery.Dump("FinalQuery");

LINQ Dynamic Query

using System.Linq.Dynamic

var repository = new EmployeeRepository();

var query = repository.GetAll()
.AsQueryable()
.OrderBy("Name")
.Where("DepartmentID = 1");

LINQ Joins


var employeeRepository = new EmployeeRepository();
var departmentRepository = new DepartmentRepository();

Inner Join (Normal)

var employees =
from employee in employeeRepository.GetAll()
join department in departmentRepository.GetAll()
on employee.DepartmentID equals department.ID
select new {employee.Name, Department = department.Name};

Left Join (Grouping on Departments)

var query =
from d in departmentRepository.GetAll()
join e in employeesRepository.GetAll()
on d.ID equals e.DepartmentID
into ed
select new
{
Department = d.Name,
Employees = ed};

foreach(var group in query)
{
Console.WriteLine(group.Department);
foreach(var employee in group.Employees)
{
Console.WriteLine("\t" + employee.Name);
}
}

Cross Join (for completeness)


var employees =
from employee in employeeRepository.GetAll()
join department in departmentRepository.GetAll()
on employee.DepartmentID equals department.ID
select new {employee.Name, Department = department.Name};

LINQ Grouping and Projecting

var repository = new EmployeeRepository();

Comprehensive Query Syntax

var queryByDepartment =
from e in repository.GetAll()
group e by e.DepartmentID
into eGroup
orderby eGroup.Key descending
where eGroup.Key < 3
select new
{
DepartmentID = eGroup.Key,
Count = eGroup.Count(),
Employees = eGroup
};

Extension Methods with Lambda Expressions

var queryByDepartment2 =
repository.GetAll()
.GroupBy(e=>e.DepartmentID)
.OrderByDescending(g=>g.Key)
.Where(g=>g.Key < 3)
.Select(g=>
new
{
DepartmentID = g.Key,
Count = g.Count(),
Employees = g
});

foreach(var group in queryByDepartment2)
{
Console.WriteLine("DID: {0}, Count: {1}",
group.DepartmentID,
group.Count);

foreach(var employee in group.Employees)
{
Console.WriteLine("\t{0}:{1}", employee.DepartmentID, employee.Name);
}
}

Extension Methods with Lambda Expressions vs. Comprehensive Query Syntax

Employee[] employees = new Employee[]
{
new Employee{ID=1, Name="The"},
new Employee{ID=2, Name="Foo"}
};

//Employee the = Array.Find(employees, FindThePredicate);
Employee the = Array.Find(employees, e=>e.Name == "The");

Comprehensive Query Syntax

Collection

IEnumerable<Employee> query1 =
from e in employees
where e.Name == "The"
orderby e.ID ascending
select e;

Single Item

Employee query1 =
(from e in employees
where e.Name == "The"
orderby e.ID ascending
select e).First();

Extension Methods with Lambda Expressions

Collection

IEnumerable<Employee> query2 =
employees.Where(e=>e.Name=="The")
.OrderBy(e=>e.ID)
.Select(e=>e);

Single Item

Employee query2 =
employees.Where(e=>e.Name=="The")
.OrderBy(e=>e.ID)
.Select(e=>e)
.First();

Wednesday, May 23, 2012

Named vs. Anonymous Method vs. Lambda Expressions

Named Method Predicate - passing method pointers

static void Main(string[] args)
{
Employee the = Array.Find(employees, FindThePredicate);
}

static bool FindThePredicate(Employee e)
{
return e.Name == "The";
}

Anonymous Method

static void Main(string[] args)
{
Employee the = Array.Find(employees,
delegate(Employee e)
{
return e.Name == "The";
}
);
}

Lambda Expression - anonymous method but with less code

static void Main(string[] args)
{
Employee the = Array.Find(employees, (e) => e.Name =="The");
}

Monday, March 31, 2008

Must-Learned Semantic Tools

W3C Semantic WebOne of my interests is in Semantic Web. The following tools are great if you already know .NET, C#, RSS and RDF, and want to do more with Semantic Web.

Semantic Web/RDF Library for C#/.NET


SemWeb is my Semantic Web/RDF library written in C# for Mono or Microsoft's .NET 1.1/2.0. The library can be used for reading and writing RDF (XML, N3), keeping RDF in persistent storage (memory, MySQL, etc.), querying persistent storage via simple graph matching and SPARQL, and making SPARQL queries to remote endpoints. Limited RDFS and general-purpose inferencing is also possible. SemWeb's API is straight-forward and flexible.

ASP.NET RSS Toolkit


RssDataSource control to consume feeds in ASP.NET applications:

  • Works with ASP.NET data bound controls

  • Implements schema to generate columns at design time

  • Supports auto-generation of columns at runtime (via ICustomTypeDescriptor implementation)


Caching of downloaded feeds both in-memory and on-disk (persisted across process restarts)
Generation of strongly typed classes for feeds (including strongly typed channel, items, image, handler) based on a feed URL (the toolkit recognizes RSS, Atom and RDF feeds) or a file containing a sample feed. Allows programmatically download (and creation) of feeds using strongly-typed classes.

LinqToRdf


LinqToRdf provides a full-featured LINQ query provider for .NET using both local triple stores with Graph Matching and SPARQL queries on remote stores. It also provides graphical design tools for visual studio 2008 that provide a UML style design surface for the production of both ontologies and .NET domain models.