tags:

views:

1265

answers:

3

I have a query that works fine when using an anonymous type but as soon as I try to un-anonymize it it fails to select all values into the class.

here is the linq i'm using (in combination with Subsonic 3):

var producten = (from p in Premy.All()
     join pr in Producten.All() on p.dekking equals pr.ID
     where p.kilometragemax >= 10000 &&
           p.CCmin < 3000 &&
           p.CCmax >= 3000 &&
           p.leeftijdmax >= DateTime.Today.Subtract(car.datumEersteToelating).TotalDays / 365
     group p by new { pr.ID, pr.Naam, pr.ShortDesc, pr.LongDesc } into d
     select new
     {
         ID = d.Key.ID,
         Dekking = d.Key.Naam,
         ShortDesc = d.Key.ShortDesc,
         LongDesc = d.Key.LongDesc,
         PrijsAlgemeen = d.Min(x => x.premie),
         PrijsAlgemeenMaand = d.Min(x => x.premie),
         PrijsMerkdealerMaand = d.Min(x => x.premie),
         PrijsMerkdealer = d.Min(x => x.premie)
     }).ToList();

When I change it to:

List<QuotePremies> producten = (from p in Premy.All()
     join pr in Producten.All() on p.dekking equals pr.ID
     where p.kilometragemax >= 10000 &&
           p.CCmin < 3000 &&
           p.CCmax >= 3000 &&
           p.leeftijdmax >= DateTime.Today.Subtract(car.datumEersteToelating).TotalDays / 365
     group p by new { pr.ID, pr.Naam, pr.ShortDesc, pr.LongDesc } into d
     select new QuotePremies
     {
         ID = d.Key.ID,
         Dekking = d.Key.Naam,
         ShortDesc = d.Key.ShortDesc,
         LongDesc = d.Key.LongDesc,
         PrijsAlgemeen = d.Min(x => x.premie),
         PrijsAlgemeenMaand = d.Min(x => x.premie),
         PrijsMerkdealerMaand = d.Min(x => x.premie),
         PrijsMerkdealer = d.Min(x => x.premie)
     }).ToList();

in combination with this class:

public class QuotePremies
{
    public byte ID { get; set; }
    public string Dekking { get; set; }
    public string ShortDesc { get; set; }

    public string LongDesc { get; set; }
    public decimal PrijsAlgemeen { get; set; }
    public decimal PrijsAlgemeenMaand { get; set; }
    public decimal PrijsMerkdealer { get; set; }
    public decimal PrijsMerkdealerMaand { get; set; }
}

it doesn't give me an error but all values in the class are 0 except for QuotePremies.ID, QuotePremies.ShortDesc and QuotePremies.LongDesc. No clue why that happens.

+1  A: 

See if using conversion helps

PrijsAlgemeen = Convert.ToDecimal(d.Min(x => x.premie))
shahkalpesh
nope, doesn't work.
Yannick Smits
Is the value of PrijsAlgemeen set correctly, if you use an anonymous type?
shahkalpesh
yes it is correctly set.
Yannick Smits
Try casting. (Decimal)(d.Min(x => x.premie)). Does that work?
shahkalpesh
nope, doesn't work.
Yannick Smits
There should be an InvalidCastException if that was the issue.
Richard Hein
A: 

I believe the problem has to do with casting. Why not write and extension method for IEnumberable which would take this query result and return a collection of List. It could look something like this:

public static class Extensions
{
    // extends IEnumerable to allow conversion to a custom type
    public static TCollection ToMyCustomCollection<TCollection, T>(this IEnumerable<T> ienum)
           where TCollection : IList<T>, new()
    {
        // create our new custom type to populate and return
        TCollection collection = new TCollection();

        // iterate over the enumeration
        foreach (var item in ienum)
        {
            // add to our collection
            collection.Add((T)item);
        }

        return collection;
    }
}

Thanks to kek444 for helping me with a similar problem

Tony D
thanks for the suggestion but unfortunately I don't understand how that works. I'm an oldscool C# 1.0 guy struggling with all these new things, barely even understanding generics. Custom types, Extensions IEnumerable are making me feel dizzy...
Yannick Smits
+1  A: 

I'm suffering with exaclty same issue. It's a bug in Subsonic: http://stackoverflow.com/questions/1734146/subsonic-3-linq-projecting-anonymous-types-but-not-class-types

Apocatastasis