在linq之间有区别:
EFDbContext _db = new EFDbContext();
1)_db.UserQuizes
.Where(uq => uq.UserId == currentUserId && uq.QuizId == quizId)
.Include(qz => qz.Quiz.VerbalQuizes.Select(q => q.Question)).First()
2)_db.UserQuizes
.Include(qz => qz.Quiz.VerbalQuizes.Select(q => q.Question))
.Where(uq => uq.UserId == currentUserId && uq.QuizId == quizId).First()
3)_db.UserQuizes
.Include(qz => qz.Quiz.VerbalQuizes.Select(q => q.Question))
First(uq => uq.UserId == currentUserId && uq.QuizId == quizId)
Run Code Online (Sandbox Code Playgroud)
请注意,第一个查询使用包括在where之前和之后的where,但结果是相同的.另外如何查看实际的sql查询?在这个特殊情况下,性能是我的主要目标,我可以改进查询吗?我需要更改两个属性:UserQuizes属性和UserQuizes-> VerbalQuizes-> Question属性.
将它分成两个查询或者像它一样使用它会更好吗?
我正在使用ASP.NET身份会员资格.这是Startup.Auth.cs代码:
app.CreatePerOwinContext(EFDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
ExpireTimeSpan = TimeSpan.FromHours(3),
CookieName = "MyLoginCookie",
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user …Run Code Online (Sandbox Code Playgroud) 在 linq 中是否可以在匿名选择中增加变量?
例如,这是我的代码:
int[] coefficients = { 1, 2, 3, };
int i = 0;
var verbalResult =
from fq in finishedQuizzes
from vq in fq.Quiz.VerbalQuizes
group vq by vq.Question.ExamTagId into r
select new
{
Tag = r.First().Question.ExamTag.Name,
CorrectAnswerCount = r.Sum(e => e.ISMovedAnswerCorrect ? 1 : 0),
questionCount = r.Count(),
coefficient = coefficients[i],
i++
};
Run Code Online (Sandbox Code Playgroud)
我想通过索引(i)获得系数。我可以i像这样增加变量吗?