在单个数据库调用中,在EF模型LINQ中获取多个计数。

3
我正在使用LINQ EF模型从数据库中获取值。我需要获取某些行的计数,我正在使用以下代码进行相同操作。
for (int i = 0; i < optionsList.Length; i++)
{
     var map = new Dictionary<string, double>();
     int count = _db.UserSurveyResultToBeRevieweds1
                    .Where(x=>x.refKey == singleQuestionsLists.referenceKey && 
                              x.type == questionTypeSAQ && 
                              x.questionId == Convert.ToString(singleQuestionsLists.Id) &&  
                              x.answer == Convert.ToString(i)).Count();
     // need to use count in map
}

这将调用数据库i次。有没有可能在一次数据库调用中获取总数?如果i的值很大,这会影响代码性能吗?

2个回答

2
您可以使用GroupBy,如下所示:
var map = new var map = new Dictionary<string, double>();
var groups = _db.UserSurveyResultToBeRevieweds1
                .Where(x=>x.refKey == singleQuestionsLists.referenceKey && 
                          x.type == questionTypeSAQ && 
                          x.questionId == Convert.ToString(singleQuestionsLists.Id)
                .GroupBy(x=>x.answer);

foreach(var g in groups)
    map.Add(g.Key, g.Count()); 

或者用更少的代码:

var map = _db.UserSurveyResultToBeRevieweds1
             .Where(x=>x.refKey == singleQuestionsLists.referenceKey && 
                       x.type == questionTypeSAQ && 
                       x.questionId == Convert.ToString(singleQuestionsLists.Id)
             .GroupBy(x=>x.answer)
             .ToDictionary(x=>x.Key, x=>x.Count());

更新

上述代码片段中需要改变的一件事是在linq查询内部使用Convert.ToString方法。可以按照以下方式进行更改:

string questionListId = Convert.ToString(singleQuestionsLists.Id);

然后在你的linq查询中:

var map = _db.UserSurveyResultToBeRevieweds1
             .Where(x=>x.refKey == singleQuestionsLists.referenceKey && 
                       x.type == questionTypeSAQ && 
                       x.questionId == questionListId 
             .GroupBy(x=>x.answer)
             .ToDictionary(x=>x.Key, x=>x.Count());

你的代码运行良好,唯一需要更改的是不能在LINQ查询内部进行int到string的转换。 请将其更改为 var questionListId = Convert.ToString(singleQuestionsLists.Id); - Sumit Chourasia
请编辑您的答案,这样其他人就不必为同样的问题烦恼了。 - Sumit Chourasia
@SumitChourasia 好的,我现在就去做。谢谢! - Christos

1
你可以使用Contains
 var answers = optionsList.Select((v, i) => Convert.ToString(i)).ToArray();
 int totalCount = _db.UserSurveyResultToBeRevieweds1
                .Where(x=>x.refKey == singleQuestionsLists.referenceKey && 
                          x.type == questionTypeSAQ && 
                          x.questionId == Convert.ToString(singleQuestionsLists.Id) &&  
                          answers.Contains(x.answer)).Count();

但是,我担心你的代码中涉及到的 Convert.ToString(singleQuestionsLists.Id) 可能无法转换为 linq 表达式。

正确写法应该是:

var questionListId = Convert.ToString(singleQuestionsLists.Id);

并像这样使用它。

x.questionId == questionListId

完整代码

 var answers = optionsList.Select((v, i) => Convert.ToString(i)).ToArray();
 var questionListId = Convert.ToString(singleQuestionsLists.Id);
 int totalCount = _db.UserSurveyResultToBeRevieweds1
                .Where(x=>x.refKey == singleQuestionsLists.referenceKey && 
                          x.type == questionTypeSAQ && 
                          x.questionId == questionListId &&  
                          answers.Contains(x.answer)).Count();

谢谢。是的,在我们可以在LINQ表达式中使用之前,需要将整数转换为字符串。 - Sumit Chourasia
你的代码也很好用。但是我不能同时选择两个作为答案 :( - Sumit Chourasia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接