我想知道这里是否有人知道我为什么会得到例外
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
行。
答案 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 }
);