[英]Property is not supported in LINQ-to-entities
我有问题,我不知道如何解决。 我已经看过数百万这样的帖子,但对我没有帮助。
我有这个:
public interface IInterface
{
int Order { get; set; }
}
public abstract class AbstractClass { }
和两个实现
public class FirstImplementation : AbstractClass, IInterface
{
[Column(nameof(IInterface.Order))]
public int Order { get; set; }
}
public class SecondImplementation : AbstractClass, IInterface
{
[Column(nameof(IInterface.Order))]
public int Order { get; set; }
}
和其他实现,但它们没有Order
属性。
public class MyContext
{
public DbSet<AbstractClass> AbstratClass { get; set; }
}
我不能将属性Order
放在AbstractClass
中,导致其他逻辑并且因为其他实现没有该属性,我尝试将这些解决方案与ColumnAttribute
一起放在一列中以 map 解决。
但是当我尝试这些查询时,它抛出一个异常:
System.NotSupportedException: '实体的 LINQ 不支持指定的类型成员 'Order'。 仅支持初始值设定项、实体成员和实体导航属性。
查询:
myContext.AbstractClass
.OfType<FirstImplementation>()
.Concat<IInterface>(myContext.AbstractClass
.OfType<SecondImplementation>())
.Max(x => x.Order);
目标应该是使用IQueryable<T>
执行Max
我在 stackoverflow 中尝试了一些解决方案,但没有用
编辑:工作查询:
myContext.AbstractClass.OfType<FirstImplementation>().Max(x => x.Order)
或者
myContext.AbstractClass.OfType<FirstImplementation>().AsQueryable<IInterface>().Max(x => x.Order)
当我Concat
时抛出异常
您的上下文仅包含AbstractClass
的DbSet
,但不包含FirstImplementation
。 根据此OfType<DerivedType>
仅适用于实体框架,如果DerivedType
已知DbContext
(请参阅 smitpatel 的第一条评论)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.