繁体   English   中英

类型'<> f__AnonymousType2`2 [System.String,System.Int32]'必须声明一个默认(无参数)构造函数

[英]The type '<>f__AnonymousType2`2[System.String,System.Int32]' must declare a default (parameterless) constructor

我想知道这里是否有人知道为什么我得到例外

System.InvalidOperationException: The type '<>f__AnonymousType2`2[System.String,System.Int32]' must declare a default (parameterless) constructor in order to be constructed during mapping.

从我的LINQ-to-SQL查询中抛出

            var query =
                (
                    from change in context.ProductChangeTrackings
                    from expression in context.ProductDataQualityRuleExpressions
                    join rule in context.ProductDataQualityRules
                    on expression.ProductDataQualityRuleID equals rule.ProductDataQualityRuleID
                    where
                    (
                        from result in context.ProductDataQualityResults
                        where result.SKU == change.SKU
                        select result.ProductRowVersion
                    ).Any(rv => rv == change.RowVersion)
                    && rule.Status == "Active"
                    select new { change.SKU, rule.ProductDataQualityRuleID }
                );

我正在经历它

    public async Task<IEnumerable<T>> ExecuteAsync<T>(IQueryable<T> query, CancellationToken token = default(CancellationToken))
    {
        var cmd = (SqlCommand)this.GetCommand(query);

        if(cmd.Connection.State == ConnectionState.Closed)
        {
            await cmd.Connection.OpenAsync(token);
        }

        var reader = await cmd.ExecuteReaderAsync(token);

        return this.Translate<T>(reader);
    }

它扔在return线上。

我怀疑这与DB访问(而不是LINQ选择)有关,​​我发现这可能会有所帮助:

http://iansrobinson.com/2008/05/19/linq-to-sql-some-puzzles/

我相信您的问题与Translate 创建一个类来保存查询结果:

public class Result_SKU_QualityRule {
    public string SKU;
    public int ProductDataQualityRuleID;
}

然后在查询中使用它:

var query = (from change in context.ProductChangeTrackings
             ...
             select new Result_SKU_QualityRule { SKU = change.SKU, ProductDataQualityRuleID = rule.ProductDataQualityRuleID }
            );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM