Today I opened a Web Application (developed by someone else) from a client and tried to build it in Visual Studio 2008 and I got this build error:
Could not load file or assembly 'System.Data.DataSetExtensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
Upon further investigation I figured out that the Framework 3.5 has changed the System.Data.DataSetExtensions from version 2.0.0.0 to 3.5.0.0. To resolve the problem,
1. Right-Click on the Web project and select "Property pages", The following window will load up and we can clearly see in the following screenshot that the System.Data.DataSetExtensions assembly is of the version 2.0.0.0.
Tuesday, May 6, 2008
System.Data.DataSetExtensions Build Error
In this blog article I would like a take a simple code example in C# and explore the different ways of expressing it using LINQ, Anonymous methods and Lambda Expressions.
Code Sample:
Let's start by defining a simple class Person
public class Person
{
public Person(string name, int age)
{
if(!string.IsNullOrEmpty(name) & age > 0)
{
Name = name;
Age = age;
}
}
public string Name { get; set; }
public int Age { get; set; }
}
{
public Person(string name, int age)
{
if(!string.IsNullOrEmpty(name) & age > 0)
{
Name = name;
Age = age;
}
}
public string Name { get; set; }
public int Age { get; set; }
}
As first step let's create a List<Person> and initialize with some people as follows:
static void Main(string[] args)
{
PopulateSample();
}
private static void PopulateSample()
{
persons = new List<Person>();
persons.Add(new Person("Joe", 40));
persons.Add(new Person("Steve", 35));
persons.Add(new Person("Mike", 29));
persons.Add(new Person("Dan", 45));
persons.Add(new Person("Jeff", 30));
}
{
PopulateSample();
}
private static void PopulateSample()
{
persons = new List<Person>();
persons.Add(new Person("Joe", 40));
persons.Add(new Person("Steve", 35));
persons.Add(new Person("Mike", 29));
persons.Add(new Person("Dan", 45));
persons.Add(new Person("Jeff", 30));
}
Now that we have some data to work with as a next step let's try to find all the people in the list whose age is above 31 and print their names. As soon as try to write a solution to this, the first thing that comes to my mind is iterating through the list using a foreach loop. A very basic code sample would be as follows:
private static void ExecuteSampleUsingForEach()
{
Console.WriteLine("The Result using Foreach method:");
foreach(Person p in persons)
{
if(p.Age > 31)
{
Console.WriteLine(p.Name);
}
}
}
{
Console.WriteLine("The Result using Foreach method:");
foreach(Person p in persons)
{
if(p.Age > 31)
{
Console.WriteLine(p.Name);
}
}
}
There is absolutely nothing wrong with this approach. But there are different methods of representing the same thing in code. The following methods may look very different but they all yield the same result.
private static void ExecuteSampleUsingAnonymousMethod()
{
IEnumerable<Person> peopleWithLongNames = persons.FindAll(delegate(Person p){return p.Age > 31; });
Console.WriteLine("The Result using Anonymous Method:");
foreach(Person person in peopleWithLongNames)
{
Console.WriteLine(person.Name);
}
}
private static void ExecuteSampleUsingLINQ()
{
IEnumerable<Person> peopleWithLongNames = from p in persons where p.Age > 31 select p;
Console.WriteLine("The Result using LINQ:");
foreach(Person person in peopleWithLongNames)
{
Console.WriteLine(person.Name);
}
}
private static void ExecuteSampleUsingLambdaExpressions()
{
IEnumerable<Person> peopleWithLongNames = persons.Where(p => p.Age > 31);
Console.WriteLine("The Result using Lambda Expressions:");
foreach(Person person in peopleWithLongNames)
{
Console.WriteLine(person.Name);
}
}
{
IEnumerable<Person> peopleWithLongNames = persons.FindAll(delegate(Person p){return p.Age > 31; });
Console.WriteLine("The Result using Anonymous Method:");
foreach(Person person in peopleWithLongNames)
{
Console.WriteLine(person.Name);
}
}
private static void ExecuteSampleUsingLINQ()
{
IEnumerable<Person> peopleWithLongNames = from p in persons where p.Age > 31 select p;
Console.WriteLine("The Result using LINQ:");
foreach(Person person in peopleWithLongNames)
{
Console.WriteLine(person.Name);
}
}
private static void ExecuteSampleUsingLambdaExpressions()
{
IEnumerable<Person> peopleWithLongNames = persons.Where(p => p.Age > 31);
Console.WriteLine("The Result using Lambda Expressions:");
foreach(Person person in peopleWithLongNames)
{
Console.WriteLine(person.Name);
}
}
As a developer one of my goals is to write less but elegant code. With Framework 3.5 C# gives so many options to do the same thing. As developers it's up to us to use the right way at the right place.
Labels:
C#
Subscribe to:
Posts (Atom)

