double values and lambdas

May 5, 2009 at 2:55 PM

Hi.

I'm using with your project NLinq and I'm having some problems with double values. I've seen you work only with int values on methods Min, Max, Sum, Average, ...  and changed to work with double values, but I'm still having problem with lambda expressions, for example:

new NLinqQuery("lst.Sum()") worked fine, but
new NLinqQuery("lst.Sum(n => n<5)") doesn't work...

Is there a way to get it working?
Thanks.

Coordinator
Aug 9, 2009 at 10:53 AM

Actually I think that using Sum() with a boolean expression inside is not allowed. If you want to filter the numbers to sum, you should add a Where() statement before. The only lambda which is allowed is a projection, to select a specific property of the elements in the list. Here is the example taken from the tests:

 

[Test]
public void SumSimple()
{
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

    query = new NLinqQuery(@"
        numbers.Sum()
    ");

    linq = new LinqToMemory(query);
    linq.AddSource("numbers", numbers);

    int result = linq.Evaluate();

    Assert.AreEqual(45, result);
}

[Test]
public void SumProjection()
{
    string[] words = { "cherry", "apple", "blueberry" };

    query = new NLinqQuery(@"
        words.Sum(w => w.Length)
    ");

    linq = new LinqToMemory(query);
    linq.AddSource("words", words);

    int result = linq.Evaluate();

    Assert.AreEqual(20, result);
}