gpt4 book ai didi

c# - Linq 查询在编译时不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:51 25 4
gpt4 key购买 nike

我有超过 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 cannot be null. Parameter name: value

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: value

但是当我运行相同的代码而不编译它时,它工作正常,问题是什么?

这很好用:

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 cannot be null. Parameter name: value] System.Data.Linq.SqlClient.SqlJoin..ctor(SqlJoinType type, SqlSource left, SqlSource right, SqlExpression cond, Expression sourceExpression) +1222094 System.Data.Linq.SqlClient.Visitor.VisitMultiset(SqlSubSelect sms) +324 System.Data.Linq.SqlClient.SqlVisitor.VisitSubSelect(SqlSubSelect ss) +91 System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) +1014 System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp) +15 System.Data.Linq.SqlClient.SqlVisitor.VisitNew(SqlNew sox) +186 System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) +1205 System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp) +15 System.Data.Linq.SqlClient.Visitor.VisitSelect(SqlSelect select) +128 System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) +1110 System.Data.Linq.SqlClient.SqlProvider.BuildQuery(ResultShape resultShape, Type resultType, SqlNode node, ReadOnlyCollection`1 parentParameters, SqlNodeAnnotations annotations) +828 System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations) +279 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Compile(Expression query) +104 System.Data.Linq.CompiledQuery.ExecuteQuery(DataContext context, Object[] args) +203 System.Data.Linq.CompiledQuery.Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4) +223 comments.Page_Load(Object sender, EventArgs e) in g:\Dropbox\Projects\NameBabies-New\comments.aspx.cs:21 System.Web.UI.Control.LoadRecursive() +71 System.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 代码以不受控制的方式崩溃这一事实可以清楚地看出这一点。

也就是说,查询子集合(此处:评论)在工作时速度很慢 (SELECT N+1) 或不受支持。可能,编译查询不支持它。编译查询不支持某些结构。

出于性能方面的考虑,您可能无论如何都不应该查询子集合。重写您的代码,以免发生这种情况。

因为您可能正在学习 .NET:LINQ to SQL 已过时,许多人认为 ASPX 页面已过时(或不得已的技术)。

关于c# - Linq 查询在编译时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31476409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com