2009-08-12 52 views
3

我有一个查询在使用匿名类型时可以正常工作,但只要我试图对其匿名化,就无法将所有值选择到类中。无法使用linq选择类

这里是我使用(联合亚音速3)LINQ:与此类

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(); 

组合:

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; } 
} 

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(); 

当我将其更改为

它不会给我一个错误,但类中的所有值都是0,除了QuotePremies.ID,QuotePremies.ShortDesc和Quo tePremies.LongDesc。不知道为什么会发生。

+0

我没有亚音速3任何经验,但我要说,这是一个错误...... – Thorarin 2009-08-12 21:55:07

+0

你能在不同的应用程序bug重现,在较短的方式? – 2009-08-13 00:08:07

+1

'x.premie'是什么类型? – SLaks 2009-08-13 03:03:25

回答

1

看看是否使用转换有助于

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

不行,不起作用。 – 2009-08-12 21:54:24

+0

如果您使用匿名类型,是否正确设置了PrijsAlgemeen的值? – shahkalpesh 2009-08-12 22:05:32

+0

是的,它被正确设置。 – 2009-08-12 22:13:39

0

我相信问题与铸件做。为什么不为IEnumberable编写和扩展方法,它将采用此查询结果并返回List的集合。它可能是这个样子:

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; 
    } 
} 

感谢kek444帮助我类似的问题