本文介绍了实体框架映射枚举:指定的值不是“Edm.Int32”类型的实例参数name:value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将实体框架查询的结果返回到我自己的dto类中,同时映射我的枚举TradeType。



我是获取以下错误



指定的值不是Edm.Int32类型的实例参数名称:值



任何想法如何修复或解决方法?



谢谢

  public IEnumerable&Trade GetLiveTrades(string routeName)
{
return _entities.Price.Where(p => p.StatusCode.Equals(A)& p.Period.PeriodYear< = DateTime.Now .Year + 1&& p.Route.RouteCode.Equals(routeName))。
选择(p => new Trade
{
Volume =(long)(p.Volume ?? 100),
TradeType =(p.PriceTypeCode.Equals )?TradeType.Seller:TradeType.Bidder),
Price = p.Price1,
TenorStartDate = p.Period.PeriodStartDate.Value,
TenorEndDate = p.Period.PeriodStartDate.Value,
TradeId = p.ID
})ToList();
}

public class Trade
{
public long Volume {get;组; }
public TradeType TradeType {get;组; }
public double价格{get;组; }
public DateTime TenorStartDate {get;组; }
public DateTime TenorEndDate {get;组; }
public Guid TradeId {get;组; }
}


解决方案

枚举来自实体的预测框架预测(选择)是一个知道的问题。如果你这样做的话,那么你可以通过这个方式来实现。 ;
p.Period.PeriodYear< = DateTime.Now.Year + 1&&b
p.Route.RouteCode.Equals(routeName))ToList()// ToList!
。选择(p => new Trade ...

投影由






编辑

>

作为一个迟到的想法,我想补充说,只要一个愚蠢的 ToList()可能会对有问题的表有害有很多列,这意味着更多的数据被传递给客户端,在这种情况下,做一个双投影可能是有用的首先,在查询提供者项目的范围内接受的类型,然后在切换到linq-to-objects( AsEnumerable )项目到最终类型后:

  _entities.Price.Where(p => p.StatusCode.Equals(A)&& 
p.Period.PeriodYear< = DateTime.Now.Year + 1&
p.Route.RouteCode.Equals(routeName))
.Select(p => new
{
卷=(long)(p。音量? 100),
PriceTypeCode = p.PriceTypeCode,
Price = p.Price1,
TenorStartDate = p.Period.PeriodStartDate.Value,
TenorEndDate = p.Period.PeriodStartDate.Value ,
TradeId = p.ID
})
.AsEnumerable()
.Select(x => new Trade
{
Volume = x.Volume,
TradeType =(x.PriceTypeCode.Equals(O)?TradeType.Seller:TradeType.Bidder),
Price = x.Price1,
TenorStartDate = x.TenorStartDate,
TenorEndDate = x.TenorEndDate,
TradeId = x.ID
})。


I'm trying to return the result of a entity framework query into my own dto class, at the same time as mapping my enum TradeType.

I'm getting the following error

The specified value is not an instance of type 'Edm.Int32' Parameter name: value

Any idea how to fix or a workaround?

Thanks

public IEnumerable<Trade> GetLiveTrades(string routeName)
{
    return _entities.Price.Where(p => p.StatusCode.Equals("A") && p.Period.PeriodYear <= DateTime.Now.Year+1 && p.Route.RouteCode.Equals(routeName)).
        Select(p => new Trade
                        {
                            Volume = (long) (p.Volume ?? 100), 
                            TradeType = (p.PriceTypeCode.Equals("O") ? TradeType.Seller : TradeType.Bidder),
                            Price = p.Price1,
                            TenorStartDate = p.Period.PeriodStartDate.Value,
                            TenorEndDate = p.Period.PeriodStartDate.Value,
                            TradeId = p.ID
                        }).ToList();
        }

public class Trade
{
    public long Volume { get; set; }
    public TradeType TradeType { get; set; }
    public double Price { get; set; }
    public DateTime TenorStartDate { get; set; }
    public DateTime TenorEndDate { get; set; }
    public Guid TradeId { get; set; }
}
解决方案

Enums in projections from Entity Framework projections (Select) is a know issue. If you do

_entities.Price.Where(p => p.StatusCode.Equals("A") &&
    p.Period.PeriodYear <= DateTime.Now.Year + 1 &&
    p.Route.RouteCode.Equals(routeName)).ToList() // ToList !
    .Select(p => new Trade ...

the projection is done by regular linq-to-objects, which is a routine job.


EDIT

As a late afterthought I'd like to add that just a dumb ToList() can be detrimental when the table in question has many columns. It would mean that far more data is transferred to the client than necessary. In such cases it can be useful to do a double projection. First, within the scope of the query provider project to accepted types. Then, after switching to linq-to-objects (AsEnumerable) project to the final type:

_entities.Price.Where(p => p.StatusCode.Equals("A") &&
    p.Period.PeriodYear <= DateTime.Now.Year + 1 &&
    p.Route.RouteCode.Equals(routeName))
    .Select(p => new 
                {
                  Volume = (long) (p.Volume ?? 100), 
                  PriceTypeCode = p.PriceTypeCode,
                  Price = p.Price1,
                  TenorStartDate = p.Period.PeriodStartDate.Value,
                  TenorEndDate = p.Period.PeriodStartDate.Value,
                  TradeId = p.ID
                })
    .AsEnumerable()
    .Select(x => new Trade
                 {
                   Volume = x.Volume, 
                   TradeType = (x.PriceTypeCode.Equals("O") ? TradeType.Seller : TradeType.Bidder),
                   Price = x.Price1,
                   TenorStartDate = x.TenorStartDate,
                   TenorEndDate = x.TenorEndDate,
                   TradeId = x.ID
                 }).

这篇关于实体框架映射枚举:指定的值不是“Edm.Int32”类型的实例参数name:value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 03:37