在单个DB调用中获得EF Model LINQ的多次计数

时间:2014-08-20 06:54:07

标签: c# sql linq entity-framework

我正在使用LINQ EF Model从数据库中获取价值。我要获取某些行的计数,并且我正在使用以下代码。

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 个答案:

答案 0 :(得分: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());

答案 1 :(得分: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();