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.

Yannick Smits
  • 875
  • 9
  • 14
  • I don't have any experience with SubSonic 3, but I'd say that's a bug... – Thorarin Aug 12 '09 at 21:55
  • Can you reproduce the bug in a shorter manner in a different app? – Yuriy Faktorovich Aug 13 '09 at 00:08
  • 1
    What type is `x.premie`? – SLaks Aug 13 '09 at 03:03
  • I tried to duplicate your code as much as possible without your backend, and it works fine. There's something else wrong. What type is premie, as SLaks asked? Also, why is your ID a byte? – Richard Anthony Hein Aug 13 '09 at 18:21
  • Maybe you should change it back to using anonymous types and see if it still gives you the results you want. I see no difference and suspect your data is different than you think it is. The minimum premie in the group d is 0. Take out your where conditions and see what you get. Try the Max too. – Richard Anthony Hein Aug 13 '09 at 18:27

2 Answers2

1

See if using conversion helps

PrijsAlgemeen = Convert.ToDecimal(d.Min(x => x.premie))
shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
0

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
  • 1,531
  • 17
  • 29