【问题标题】:can't select into class using linq无法使用 linq 选择上课
【发布时间】:2009-08-12 21:19:07
【问题描述】:

我有一个查询在使用匿名类型时可以正常工作,但是一旦我尝试对其取消匿名化,它就无法将所有值选择到类中。

这是我正在使用的 linq(与 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();

当我将其更改为:

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

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

【问题讨论】:

  • 我对 SubSonic 3 没有任何经验,但我会说这是一个错误......
  • 您能否在不同的应用程序中以更短的方式重现该错误?
  • x.premie是什么类型?
  • 我尝试在没有您的后端的情况下尽可能多地复制您的代码,并且效果很好。还有什么问题。正如 SLaks 所问的,premie 是什么类型的?另外,为什么你的 ID 是一个字节?
  • 也许你应该把它改回使用匿名类型,看看它是否仍然给你想要的结果。我看不出有什么不同,并且怀疑您的数据与您想象的不同。 d 组中的最低溢价为 0。拿出你的 where 条件,看看你得到了什么。也试试 Max。

标签: c# linq subsonic3


【解决方案1】:

看看使用转换是否有帮助

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

【讨论】:

  • 如果使用匿名类型,PrijsAlgemeen 的值是否设置正确?
  • 尝试投射。 (十进制)(d.Min(x => x.premie))。这行得通吗?
  • 如果这是问题,应该有一个 InvalidCastException。
【解决方案2】:

我认为问题与演员表有关。为什么不为 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 帮助我解决类似问题

【讨论】:

    猜你喜欢
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多