tags:

views:

50

answers:

3

Object is simple, it's a rate of pay:

public class RateOfPay
{
    public decimal Rate { get; set; }
    public DateTime Start { get; set; }
    public DateTime? End { get; set; }
}

and I'm trying to get it like this:

IEnumerable<T> rates = GetRates(); 
/*  
    actual collection is DevExpress XPCollection<RateOfPay> 
    which implements IEnumerable<T>
*/

var rate = from r in rates where r.End == null select r; // err here

It's odd because the intellisense works fine on r, but it's saying that r is an IEnumerable collection?

What am I missing?

A: 

In this code sample the IEnumerable<T> collection is not bound to a particular type. Hence there is no way to call the End property on the value. Is there a type in the code?

If not the following could fix the problem

var rate = from r in rates.Cast<RateOfPay>() where r.End == null select r;

If on the other hand there is just a type and you are looking to have rate be a single value then try the following.

var filtered = from r in rates where r.End == null select r;
RateOfPay v1 = filtered.Single(); // If only 1 should ever match
RateOfPay v2 = filtered.First();  // if several could match and you just want 1
JaredPar
+3  A: 

It is a collection, it's an IEnumerable().Where(rate => rate.End == null) which is all rates that match that criteria.

You're missing the .FirstOrDefault() on your IEnumerable, can't tell you the statement query syntax though..

Should look like

var rate = rates.FirstOrDefault(r => r.End == null);
Jimmy Hoffa
A: 

var rate = from r in rates where r.End == null select r; // err here

Hmmmm.... Is rate really declared ar var, or is that really `Rate rate =' ??

The reason I ask, is because the linq query is going to return an IEnumerable (with likely just one item). You probably actually want:

Rate rate = (from r in rates where r.End == null select r).First();

which could be reduced to just:

Rate rate = rates.First(r=>r.End == null);
James Curran