Linq查询在编译时不起作用
本文关键字:不起作用 编译 查询 Linq | 更新日期: 2023-09-27 18:03:08
我有超过100个编译查询,但这一个导致问题。这是我的确切编译查询:
public static Func<DatabaseDataContext, int, string, int, byte, NameCommentPageResult>
GetNameComments = CompiledQuery.Compile((DatabaseDataContext db, int NameId, string UserId, int Start, byte Count)
=> new NameCommentPageResult
{
Count = db.NameComments.Count(q => q.NameId == NameId && q.VerifiedBy != "-1"),
Name = db.Names.First(n => n.ID == NameId).Name1,
Comments = db.NameComments.Where(c => c.NameId == NameId && c.VerifiedBy != "-1").Select(c => new NameCommentResult
{
Datetime = c.Datetime,
Id = c.Id,
NameId = c.NameId,
UserId = c.UserId,
UserVoted = db.NameCommentVotes.Any(v => v.UserId == UserId && v.CommentId == c.Id),
UserDisplayName = db.AspNetUserClaims.Any(cl => cl.ClaimType == "DisplayName" && cl.ClaimValue != "" && cl.User_Id == c.UserId) ? db.AspNetUserClaims.Where(cl => cl.ClaimType == "DisplayName" && cl.ClaimValue != "" && cl.User_Id == c.UserId).First().ClaimValue : db.AspNetUsers.Where(u => u.Id == c.UserId).First().UserName,
UserPhoto = db.AspNetUserClaims.Where(cl => cl.User_Id == c.UserId && cl.ClaimType == "Image").SingleOrDefault().ClaimValue,
}).OrderByDescending(o => o.Datetime).Skip(Start - 1).Take(Count).ToArray()
});
我得到错误:
Value不能为空。参数名称:value
描述:当前web请求执行过程中出现未处理的异常。请查看堆栈跟踪以获得有关错误及其在代码中的起源位置的更多信息。异常详细信息:System。ArgumentNullException:值不能为空。参数名称:value
但是当我运行相同的代码而不编译它时,它工作得很好,问题是什么?
this works fine:
int Start = 1; byte Count = 10; int NameId = 100;
using (DatabaseDataContext db = new DatabaseDataContext())
{
//Result = _Names.Comments.GetNameComments(db, Id, User == null ? "" : User.Id, 1, (byte)10);
Result = new NameCommentPageResult
{
Count = db.NameComments.Count(q => q.NameId == Id && q.VerifiedBy != "-1"),
Name = db.Names.First(n => n.ID == Id).Name1,
Comments = db.NameComments.Where(c => c.NameId == Id && c.VerifiedBy != "-1").Select(c => new NameCommentResult
{
Datetime = c.Datetime,
Id = c.Id,
NameId = c.NameId,
UserId = c.UserId,
UserVoted = db.NameCommentVotes.Any(v => v.UserId == User.Id && v.CommentId == c.Id),
UserDisplayName = db.AspNetUserClaims.Any(cl => cl.ClaimType == "DisplayName" && cl.ClaimValue != "" && cl.User_Id == c.UserId) ? db.AspNetUserClaims.Where(cl => cl.ClaimType == "DisplayName" && cl.ClaimValue != "" && cl.User_Id == c.UserId).First().ClaimValue : db.AspNetUsers.Where(u => u.Id == c.UserId).First().UserName,
UserPhoto = db.AspNetUserClaims.Where(cl => cl.User_Id == c.UserId && cl.ClaimType == "Image").SingleOrDefault().ClaimValue,
}).OrderByDescending(o => o.Datetime).Skip(Start - 1).Take(Count).ToArray()
};
}
,这是堆栈跟踪:
[ArgumentNullException: Value不能为空。]参数名称:value]System.Data.Linq.SqlClient.SqlJoin . .ctor(SqlJoinType type, SqlSource left, SqlSource right, SqlExpression cond, Expression sourceExpression) +1222094System.Data.Linq.SqlClient.Visitor。访问"multiset (SqlSubSelect sms) +324"System.Data.Linq.SqlClient.SqlVisitor。VisitSubSelect(SqlSubSelect ss) +91System.Data.Linq.SqlClient.SqlVisitor。访问(SqlNode节点)+1014System.Data.Linq.SqlClient.SqlVisitor。访问表达式(SqlExpression exp) +15System.Data.Linq.SqlClient.SqlVisitor。访问新(SqlNew sox) +186System.Data.Linq.SqlClient.SqlVisitor。访问(SqlNode节点)+1205System.Data.Linq.SqlClient.SqlVisitor。访问表达式(SqlExpression exp) +15System.Data.Linq.SqlClient.Visitor。访问select (SqlSelect select) +128System.Data.Linq.SqlClient.SqlVisitor。访问(SqlNode节点)+1110System.Data.Linq.SqlClient.SqlProvider。BuildQuery(ResultShape ResultShape, Type resultType, SqlNode节点,ReadOnlyCollection ' 1 parentParameters, SqlNodeAnnotations注释)+828System.Data.Linq.SqlClient.SqlProvider。BuildQuery(表达式查询,SqlNodeAnnotations注释)+279System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider。编译(表达式查询)+104System.Data.Linq.CompiledQuery。ExecuteQuery(DataContext context, Object[] args) +203System.Data.Linq.CompiledQuery。调用(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4) +223评论。Page_Load(对象发送者,EventArgs e) in g:'Dropbox'Projects' nameies - new 'comments.aspx.cs:21System.Web.UI.Control.LoadRecursive () + 71System.Web.UI.Page。ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178
更新:
这个有趣的代码解决了错误,但它不是正确的事情要做,谁能建议做什么代替?
public static Func<DatabaseDataContext, int, string, int, byte, NameCommentPageResult>
GetNameComments = CompiledQuery.Compile((DatabaseDataContext db, int NameId, string UserId, int Start, byte Count)
db.Names.Select(s => new NameCommentPageResult
{
Count = db.NameComments.Count(q => q.NameId == NameId && q.VerifiedBy != "-1"),
Name = db.Names.First(n => n.ID == NameId).Name1,
Comments = db.NameComments.Where(c => c.NameId == NameId && c.VerifiedBy != "-1").Select(c => new NameCommentResult
{
Datetime = c.Datetime,
Id = c.Id,
NameId = c.NameId,
UserId = c.UserId,
UserVoted = db.NameCommentVotes.Any(v => v.UserId == UserId && v.CommentId == c.Id),
UserDisplayName = db.AspNetUserClaims.Any(cl => cl.ClaimType == "DisplayName" && cl.ClaimValue != "" && cl.User_Id == c.UserId) ? db.AspNetUserClaims.Where(cl => cl.ClaimType == "DisplayName" && cl.ClaimValue != "" && cl.User_Id == c.UserId).First().ClaimValue : db.AspNetUsers.Where(u => u.Id == c.UserId).First().UserName,
UserPhoto = db.AspNetUserClaims.Where(cl => cl.User_Id == c.UserId && cl.ClaimType == "Image").SingleOrDefault().ClaimValue,
}).OrderByDescending(o => o.Datetime).Skip(Start - 1).Take(Count).ToArray()
}).First());
我编辑=> new NameCommentPageResult
到db.Names.Select(s => new NameCommentPageResult
,只得到第一个结果。请注意,db.Names
与我的查询无关,我可以使用db.AnyTableName
代替
这是一个L2S漏洞,从内部L2S代码以不受控制的方式崩溃这一事实可以清楚地看出。
也就是说,查询子集合(这里:Comments
)在工作时要么很慢(SELECT N+1),要么不支持。可能在编译查询中不支持它。编译查询不支持某些结构。
出于性能考虑,可能无论如何都不应该查询子集合。重写你的代码,这样就不会发生这种情况。
因为也许你现在正在学习。net: LINQ to SQL已经过时了,ASPX页面被许多人认为是过时的(或者是最后的技术)。