Linq Expression.Property(param,field)是“trolling”me[System.ArgumentException]={“实例属性'B.Name'未为类型A定义”}

Linq Expression.Property(param,field)是“trolling”me[System.ArgumentException]={“实例属性'B.Name'未为类型A定义”},linq,exception,lambda,expression,Linq,Exception,Lambda,Expression,再一次,我面临一个问题,这次是使用LINQ Expression builder,这次我甚至在努力寻找它不起作用的原因。我有一个数据库第一EF项目与相当多的表。对于这个特定的案例,我必须使用其中的两个-DocHead和Contragent。MyService.metadata.cs如下所示: [MetadataTypeAttribute(typeof(DocHead.DocHeadMetadata))] public partial class DocHead { // This cl

再一次,我面临一个问题,这次是使用LINQ Expression builder,这次我甚至在努力寻找它不起作用的原因。我有一个数据库第一EF项目与相当多的表。对于这个特定的案例,我必须使用其中的两个-DocHead和Contragent。MyService.metadata.cs如下所示:

[MetadataTypeAttribute(typeof(DocHead.DocHeadMetadata))]
public partial class DocHead
{

    // This class allows you to attach custom attributes to properties
    // of the DocHead class.
    //
    // For example, the following marks the Xyz property as a
    // required property and specifies the format for valid values:
    //    [Required]
    //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
    //    [StringLength(32)]
    //    public string Xyz { get; set; }
    internal sealed class DocHeadMetadata
    {

        // Metadata classes are not meant to be instantiated.
        private DocHeadMetadata()
        {
        }

        public string doc_Code { get; set; }
        public string doc_Name { get; set; }
        public string doc_ContrCode { get; set; }
        //...

        [Include]
        public Contragent Contragent { get; set; }
    }
}

[MetadataTypeAttribute(typeof(Contragent.ContragentMetadata))]
public partial class Contragent
{

    // This class allows you to attach custom attributes to properties
    // of the Contragent class.
    //
    // For example, the following marks the Xyz property as a
    // required property and specifies the format for valid values:
    //    [Required]
    //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
    //    [StringLength(32)]
    //    public string Xyz { get; set; }
    internal sealed class ContragentMetadata
    {

        // Metadata classes are not meant to be instantiated.
        private ContragentMetadata()
        {
        }

        public string Code { get; set; }
        public string Name { get; set; }
        //...
IQueryable<DocHead> docHeads = new MyEntities().DocHead;
docHeads = docHeads.OrderByDescending(x => x.Contragent.Name);
string field = "Contragent.Name";
string linq = "docHeads = docHeads.OrderByDescending(x => x." + field + ")";
IQueryable<DocHead> result = TheBestLinqLibraryInTheWorld.PrepareLinqQueryable(linq);
docHeads = docHeads.OrderByField<DocHead>("Contragent.Name", true); // true - let it be Ascending order
我拿了一些像这样的头像:

[MetadataTypeAttribute(typeof(DocHead.DocHeadMetadata))]
public partial class DocHead
{

    // This class allows you to attach custom attributes to properties
    // of the DocHead class.
    //
    // For example, the following marks the Xyz property as a
    // required property and specifies the format for valid values:
    //    [Required]
    //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
    //    [StringLength(32)]
    //    public string Xyz { get; set; }
    internal sealed class DocHeadMetadata
    {

        // Metadata classes are not meant to be instantiated.
        private DocHeadMetadata()
        {
        }

        public string doc_Code { get; set; }
        public string doc_Name { get; set; }
        public string doc_ContrCode { get; set; }
        //...

        [Include]
        public Contragent Contragent { get; set; }
    }
}

[MetadataTypeAttribute(typeof(Contragent.ContragentMetadata))]
public partial class Contragent
{

    // This class allows you to attach custom attributes to properties
    // of the Contragent class.
    //
    // For example, the following marks the Xyz property as a
    // required property and specifies the format for valid values:
    //    [Required]
    //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
    //    [StringLength(32)]
    //    public string Xyz { get; set; }
    internal sealed class ContragentMetadata
    {

        // Metadata classes are not meant to be instantiated.
        private ContragentMetadata()
        {
        }

        public string Code { get; set; }
        public string Name { get; set; }
        //...
IQueryable<DocHead> docHeads = new MyEntities().DocHead;
docHeads = docHeads.OrderByDescending(x => x.Contragent.Name);
string field = "Contragent.Name";
string linq = "docHeads = docHeads.OrderByDescending(x => x." + field + ")";
IQueryable<DocHead> result = TheBestLinqLibraryInTheWorld.PrepareLinqQueryable(linq);
docHeads = docHeads.OrderByField<DocHead>("Contragent.Name", true); // true - let it be Ascending order
一切都按照我想要的方式进行。我把那些头像按加入的反对者的名字分类。我的问题是,我必须按字段对它们进行排序,作为字符串参数给出。我需要能够写这样的东西:

[MetadataTypeAttribute(typeof(DocHead.DocHeadMetadata))]
public partial class DocHead
{

    // This class allows you to attach custom attributes to properties
    // of the DocHead class.
    //
    // For example, the following marks the Xyz property as a
    // required property and specifies the format for valid values:
    //    [Required]
    //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
    //    [StringLength(32)]
    //    public string Xyz { get; set; }
    internal sealed class DocHeadMetadata
    {

        // Metadata classes are not meant to be instantiated.
        private DocHeadMetadata()
        {
        }

        public string doc_Code { get; set; }
        public string doc_Name { get; set; }
        public string doc_ContrCode { get; set; }
        //...

        [Include]
        public Contragent Contragent { get; set; }
    }
}

[MetadataTypeAttribute(typeof(Contragent.ContragentMetadata))]
public partial class Contragent
{

    // This class allows you to attach custom attributes to properties
    // of the Contragent class.
    //
    // For example, the following marks the Xyz property as a
    // required property and specifies the format for valid values:
    //    [Required]
    //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
    //    [StringLength(32)]
    //    public string Xyz { get; set; }
    internal sealed class ContragentMetadata
    {

        // Metadata classes are not meant to be instantiated.
        private ContragentMetadata()
        {
        }

        public string Code { get; set; }
        public string Name { get; set; }
        //...
IQueryable<DocHead> docHeads = new MyEntities().DocHead;
docHeads = docHeads.OrderByDescending(x => x.Contragent.Name);
string field = "Contragent.Name";
string linq = "docHeads = docHeads.OrderByDescending(x => x." + field + ")";
IQueryable<DocHead> result = TheBestLinqLibraryInTheWorld.PrepareLinqQueryable(linq);
docHeads = docHeads.OrderByField<DocHead>("Contragent.Name", true); // true - let it be Ascending order
在My.edmx模型中,表DocHead和contracent已经为我建立了一个关系,如下所示:0..1到*

再一次,我在编写静态查询方面没有任何问题。我可以通过OrderByField方法创建动态的,但只有当涉及到类DocHead的属性时才可以。当我试图通过一个加入反政府阶级的支柱来订购时,灾难降临了。任何帮助都将不胜感激,谢谢

问题在于该方法不支持嵌套属性。它完全按照它所说的做-创建表达式,该表达式表示由表达式参数表示的对象的propertyName参数表示的属性

幸运的是,它可以很容易地扩展。需要创建嵌套属性访问表达式时,可以随时使用以下简单/技巧:

var prop = SortField.Split('.').Aggregate((Expression)param, Expression.Property);
问题是该方法不支持嵌套属性。它完全按照它所说的做-创建表达式,该表达式表示由表达式参数表示的对象的propertyName参数表示的属性

幸运的是,它可以很容易地扩展。需要创建嵌套属性访问表达式时,可以随时使用以下简单/技巧:

var prop = SortField.Split('.').Aggregate((Expression)param, Expression.Property);

我从来没有想过LINQ在欺骗我,但我对XAML有很多这种感觉。我从来没有想过LINQ在欺骗我,但我对XAML有很多这种感觉。谢谢你100%的工作解决方案!干杯@Ed Plunket——有时候,只是为了好玩,我用短语“is trolling me”替换“throws”,因为“throw”和“troll”发音非常相似。老实说,我尽最大努力避免为“tt-select中的from t”这样的数据编写linq查询,这种特殊的语法真的让我神魂颠倒使用lambdas的“普通”查询对我来说似乎更友好。感谢您100%的工作解决方案!干杯@Ed Plunket——有时候,只是为了好玩,我用短语“is trolling me”替换“throws”,因为“throw”和“troll”发音非常相似。老实说,我尽最大努力避免为“tt-select中的from t”这样的数据编写linq查询,这种特殊的语法真的让我神魂颠倒对我来说,lambdas的普通询问似乎更友好。