1

私は以下のようなクラスを持っています

public class GrouppedStockGift
{
    public DateTime? TimeCreated { get; set; }
    public int? StoreID { get; set; }
    public int? UserCreated { get; set; }
    public int? WorksID { get; set; }
    public int? WorkOrderCode { get; set; }
    public decimal? GiftPrice { get; set; }
    public decimal? GiftMoney { get; set; }
    public int SaleCount { get; set; }
}

動的式ビルダーを作成しました。場合によっては、intconvert int? to intまたはgetdefaultvalueint が必要な場合があります

var sumMethodint = typeof(Enumerable).GetMethods()
                       .Single(x => x.Name == "Sum"
                                 && x.GetParameters().Count() == 2
                                 && x.GetParameters()[1]
                                     .ParameterType
                                     .GetGenericArguments()[1] == typeof(int?));
sumMethodint = sumMethodint.MakeGenericMethod(typeof(GrouppedStockGift));
Expression<Func<GrouppedStockGift, int?>> SaleCount = y => y.SaleCount;
var SaleCountExtractor = Expression.Call(sumMethodint, parameter, SaleCount);
bindings.Add(
              Expression.Bind(
                           typeof(GrouppedStockGift).GetProperty("SaleCount"),
                           SaleCountExtractor));


しかし、最後の行を実行すると、型が一致していないため例外が返されますSaleCountが、合計メソッドは int を返しますか?
誰でも私を助けることができますか?

4

2 に答える 2

0

Expression.Call行を次のように変更するだけです

var SaleCountExtractor = Expression.Call(Expression.Call(sumMethodint, parameter, SaleCount), "GetValueOrDefault", Type.EmptyTypes);

上記のコードで

Expression.Call(exp, "GetValueOrDefault", Type.EmptyTypes);

int のデフォルト値を取得

于 2014-10-12T09:44:14.220 に答える