BdsSoft.Linq in .NET 2.0

Topics: Developer Forum
Apr 30, 2007 at 1:24 PM
Hi Bart,

When you mentioned "This allows to illustrate LINQ in a C# 2.0 context:" in your blog post on linq-sqo, I decided to give it a try, using C# 2005 Express - this is the story (read the post first)...

I commented out the hash-define CS30, but couldn't compile without a HashSet implementation. I downloaded Wintellect PowerCollections to get a HashSet class in .NET 2.0; and replace the HashSet references with Set in Union() and Distinct() - as well as add the relevant namespace.

Then I setup the test data source (person class and collection)
public class Person
{
    private string _FirstName;
    private string _LastName;
    public int Age;
    public string FirstName
    { 
        get { return _FirstName; } 
    }
    public string LastName
    {
        get { return _LastName; } 
    }
    public Person(string f, string l, int a)
    {
        _FirstName = f; _LastName = l; Age = a; 
    }
    public override string ToString()
    {
        return String.Format("(Person) {0} {1}", _FirstName, _LastName);
    }
}
public static List<Person> GetTestPeople()
{ 
    List<Person> output = new List<Person>();
    output.Add(new Person("Scott", "Guthrie", 32));
    output.Add(new Person("Bill", "Gates", 50));
    output.Add(new Person("Joe", "Smith", 22));
    return output;
}

But still couldn't get it to compile due to this error...
The type arguments for method 'BdsSoft.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, BdsSoft.Linq.Func<TSource,TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Turns out I also needed to explicity cast the delegate as follows
(Func<Person,string>)delegate(Person p) { return p.FirstName + " " + p.LastName; }

At this point the code compiled, but when I ran it the first few times I couldn't figure out why it wasn't working (hint, I was checking the values in the results, and they were all null). Of course, I wasn't enumerating over it. D'uh - I forgot about "delayed execution". Adding a foreach loop to view the results worked, and I got the output I expected: Bill Gates!

The remainder of the code I used to test follows:
IEnumerable<Person> source = GetTestPeople();
 
IEnumerable<string> results = Enumerable.Select(
                     (IEnumerable<Person>)Enumerable.Where(
                        source,
                        delegate(Person p) { return p.FirstName.StartsWith("B"); }
                     ),
                     (Func<Person,string>)delegate(Person p) { return p.FirstName + " " + p.LastName; }
                  );
 
foreach (string s in results)
{
    Console.WriteLine(s);
}

So that's a start with the underlying queries working in .NET 2.0. Now to come up with some more complex data and queries... Thanks for the code!
Coordinator
May 5, 2007 at 5:02 PM
Hi Craig,

This is a bug indeed. The need for a HashSet was added recently but the test matrix didn't run outside Orcas on a plain .NET 2.0 installation. I'll take a look into this. Thanks for the comment and your interest in the project!

Cheers,
-Bart
Coordinator
May 5, 2007 at 5:03 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jul 26, 2007 at 5:54 PM
It's been some time now. Are there any plans to add this code change to the source? I would like to evaluate this in .NET 2.0.

bdesmet wrote:
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.