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

时间:2017-06-16 21:06:10

标签: c# .net sql-server linq

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

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行。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我相信您的问题是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 }
            );