Cannot select class using linq

I have a query that works fine when using an anonymous type, but as soon as I try not to anonymize it, it cannot select all the values ​​in the class.

here is the linq I'm using (in conjunction 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 does not give me an error, but all values ​​in the class are 0, except QuotePremies.ID, QuotePremies.ShortDesc and QuotePremies.LongDesc. I don’t know why this is happening.

+3
source share
3 answers

See if using conversion helps

PrijsAlgemeen = Convert.ToDecimal(d.Min(x => x.premie))
+1
source

. Subsonic: SubSonic 3 Linq, ,

+1

I believe the problem is with casting. Why not write and extend the IEnumberable method, which would take this query result and return a List collection. It might 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

0
source

Source: https://habr.com/ru/post/1715020/


All Articles