以下是我的模型,这是我从https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/build-a-single-page-application-spa-with-aspnet-web-api-and-angularjs下载的源代码:我按照说明进行操作。
public class TriviaQuestion
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string Title { get; set; }
public virtual List<TriviaOption> Options { get; set; }
}
public class TriviaOption
{
[Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Column(Order = 0), ForeignKey("TriviaQuestion")]
public int QuestionId { get; set; }
[JsonIgnore]
public virtual TriviaQuestion TriviaQuestion { get; set; }
[JsonIgnore]
public bool IsCorrect { get; set; }
}
这是我的控制器,我收到的错误是“q.Count无法将方法组分配给匿名类型的主题。”:
private async Task<TriviaQuestion> NextQuestionAsync(string userId)
{
var lastQuestionId = await this.db.TriviaAnswers
.Where(a => a.UserId == userId)
.GroupBy(a => a.QuestionId)
.Select(g => new { QuestionId = g.Key, Count = g.Count() })
.OrderByDescending(q => new { **q.Count**, QuestionId = q.QuestionId })
.Select(q => q.QuestionId)
.FirstOrDefaultAsync();
var questionsCount = await this.db.TriviaQuestions.CountAsync();
var nextQuestionId = (lastQuestionId % questionsCount) + 1;
return await this.db.TriviaQuestions.FindAsync(CancellationToken.None, nextQuestionId);
}
答案 0 :(得分:0)
qoute
不是匿名类型字段的有效名称,您需要为其设置一个与QuestionId相同的名称
var names = ["John", "James", "Joe", "Justin"];
var quotes = [["Help me.", "Blah blah blah blah", "Blah blah blah"],["Blah blah blah", "Quote here"],["sample qoute"],["sample qoute again"]];
function whoSaidThis(qoute){
//Iterate over qoutes array
for(var i=0;i<quotes.length;i++) {
//check if any of the items in qoutes array contain the qoute
if(quotes[i].indexOf(qoute) !== -1 ) {
break;
}
}
//returns undefined if qoute not foundin the qoutes array
return i === quotes.length ? undefined : names[i] + " says : " + qoute;
};
console.log(whoSaidThis("sample qoute"));
您还可以将orderby拆分为OrderByDecending和ThenByDecending。
q.Count
如果您正在使用IQueryable对象,它最终可能会更好地查询服务器。