基于字段字符串的不同记录比较,平均字段和返回动态对象

时间:2015-06-26 19:37:37

标签: c# .net linq

我正在使用MVC 5,其中我有以下名为Question的模型/表;

enter image description here

我想产生以下结果;

enter image description here

我的目标是获取反映DISTINCT主题的记录列表,其中包含给定Distinct主题的所有分数的平均值。

var result = from q in db.Questions
               where q.Topic.Distinct()
              select new Question
              {
                  Category = q.Category,                             
                  Topic = q.Topic,                              
                  Store1Rating = db.Questions.Where(x => x.Topic == q.Topic).Average(s1 => s1.Store1Rating).ToString(),
                  Store2Rating = db.Questions.Where(x => x.Topic == q.Topic).Average(s2 => s2.Store2Rating).ToString()
              };

我知道我已经完成了LINQ声明,但我只有大约6周的基本LINQ语句。对于我来说,这是基本LINQ的另一层复杂性。提前谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用GroupBy

执行此操作
var result = db.Questions.GroupBy(x => new
//grouping data by category and topic, rows which have same category and topic will be in a group
{
    x.Category,
    x.Topic
}).Select(g => new
//now selecting category and topic for each group and the data we want
{
    g.Key.Category,
    g.Key.Topic,
    //calculate Store1Rating average of all rows which has same category and topic as this group
    Store1Rating = db.Questions.Where(x => x.Category == g.Key.Category && x.Topic == g.Key.Topic).Average(x => x.Store1Rating),
    //calculate Store2Rating average of all rows which has same category and topic as this group
    Store2Rating = db.Questions.Where(x => x.Category == g.Key.Category && x.Topic == g.Key.Topic).Average(x => x.Store2Rating),
});

答案 1 :(得分:1)

请尝试以下代码。使用以下网页作为参考:https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable Questions = new DataTable();

            Questions.Columns.Add("Id", typeof(int));
            Questions.Columns.Add("Category", typeof(string));
            Questions.Columns.Add("Topic", typeof(string));
            Questions.Columns.Add("Store1Rating", typeof(int));
            Questions.Columns.Add("Store2Rating", typeof(int));

            Questions.Rows.Add(new object[] { 1,"Food","Pizza", 5, 6 });
            Questions.Rows.Add(new object[] { 2, "Food", "Pizza", 4, 5 });
            Questions.Rows.Add(new object[] { 3, "Food", "Ice Cream", 4, 5 });
            Questions.Rows.Add(new object[] { 4, "Beverage", "Beer", 3, 4 });
            Questions.Rows.Add(new object[] { 5, "Beverage", "Soda", 4, 5 });
            Questions.Rows.Add(new object[] { 6, "Food", "Pizza", 5, 6 });
            Questions.Rows.Add(new object[] { 7, "Food", "Pizza", 4, 5 });
            Questions.Rows.Add(new object[] { 8, "Food", "Ice Cream", 3, 4 });
            Questions.Rows.Add(new object[] { 9, "Beverage", "Beer", 4, 5 });
            Questions.Rows.Add(new object[] { 10, "Beverage", "Soda", 5, 6 });

            var summary = Questions.AsEnumerable()
                .GroupBy(x => x.Field<string>("Topic"))
                .Select(x => new
                {
                    id = x.FirstOrDefault().Field<int>("Id"),
                    category = x.FirstOrDefault().Field<string>("Category"),
                    topic = x.Key,
                    rating1 = x.Select(y => y.Field<int>("Store1Rating")).ToList().Average(),
                    rating2 = x.Select(y => y.Field<int>("Store2Rating")).ToList().Average()
                }).ToList();
        }
    }
}
​