gpt4 book ai didi

c# - Automapper 投影因可空属性而失败

转载 作者:行者123 更新时间:2023-12-03 20:27:38 26 4
gpt4 key购买 nike

自动映射器版本:6.0.2

我有两个类InternalEntity作为我的数据源和PublicEntity作为 API 公开。我正在尝试访问 InternalEntity通过 PublicEntity使用 UseAsDataSource 进行投影方法。

问题是内部 bool属性映射到公共(public) Nullable<bool>属性(property)。

当我在投影中使用这个属性时它失败了,例如调用 OrderBy 。有关详细信息,请参阅下面的示例。

我可以为 bool 设置投影规则吗? -> 以某种方式 bool ?

这是代码示例:

using AutoMapper;
using AutoMapper.QueryableExtensions;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sample
{
internal class Program
{
public class PublicEntity
{
public string PublicName { get; set; }
public bool? Active { get; set; }
}
public class InternalEntity
{
public string InternalName { get; set; }
public bool Active { get; set; }
}

private static IMapper mapper;

private static void SetupMapping()
{
var mc = new MapperConfiguration(cfg =>
{
cfg.CreateMap<InternalEntity, PublicEntity>()
.ForMember(dest => dest.PublicName, opt => opt.MapFrom(src => src.InternalName));

// setup bool? -> bool projection somehow
});

mc.AssertConfigurationIsValid();
mapper = mc.CreateMapper();
}

private static void Main(string[] args)
{
SetupMapping();

try
{
IQueryable<PublicEntity> resultable = DataSource()
.UseAsDataSource(mapper)
.For<PublicEntity>()
.OrderBy(x => x.Active);

resultable.ToList()
.ForEach(x => Console.WriteLine(x.PublicName));
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}


private static IQueryable<InternalEntity> DataSource()
{
return new List<InternalEntity>()
{
new InternalEntity(){ InternalName = "Name 1", Active = true},
new InternalEntity(){ InternalName = "Name 3", Active = true},
new InternalEntity(){ InternalName = "Name 2", Active = false},
}.AsQueryable();
}
}
}

异常(exception):
System.ArgumentException: Expression of type 'System.Linq.Expressions.Expression`1[System.Func`2[Sample.Program+InternalEntity,System.Boolean]]' cannot be used for parameter of type 'System.Linq.Expressions.Expression`1[System.Func`2[Sample.Program+InternalEntity,System.Nullable`1[System.Boolean]]]' of method 'System.Linq.IOrderedQueryable`1[Sample.Program+InternalEntity] OrderBy[InternalEntity,Nullable`1](System.Linq.IQueryable`1[Sample.Program+InternalEntity], System.Linq.Expressions.Expression`1[System.Func`2[Sample.Program+InternalEntity,System.Nullable`1[System.Boolean]]])'
at System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)
at System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase method, ExpressionType nodeKind, ReadOnlyCollection`1& arguments)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
at AutoMapper.Mappers.ExpressionMapper.MappingVisitor.GetConvertedMethodCall(MethodCallExpression node)
at AutoMapper.Mappers.ExpressionMapper.MappingVisitor.VisitMethodCall(MethodCallExpression node)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at AutoMapper.QueryableExtensions.Impl.SourceInjectedQueryProvider`2.ConvertDestinationExpressionToSourceExpression(Expression expression)
at AutoMapper.QueryableExtensions.Impl.SourceInjectedQueryProvider`2.Execute[TResult](Expression expression)
at AutoMapper.QueryableExtensions.Impl.SourceSourceInjectedQuery`2.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Sample.Program.Main(String[] args) in xxxx\Program.cs:line 48

最佳答案

我设法自己找到了解决方案。一条或另一条线会做得很好:

.ForMember(dest => dest.Active, opt => opt.MapFrom(src => (bool?)src.Active))

.ForMember(dest => dest.Active, opt => opt.MapFrom(src => new Nullable<bool>(src.Active)))

这些行在语义上是等价的。

请注意,这些行会产生不同的表达式树,如果您要解析表达式树,这可能变得很重要。

关于c# - Automapper 投影因可空属性而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44648704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com