过滤一列数据的副本,但显示另一列的所有数据

时间:2011-10-13 04:53:32

标签: linq asp.net-mvc-3 razor duplicates

我正在做mvc3和linQ 我有像这样的数据库

class    student        gender

A        john           Male
B        George         Male
A        bill           Female
A        Steve          Male

我如何编码过滤副本,结果如​​下所示

A
John       male
bill       female
steve      male

B
George     male

我尝试了方法A

  var bb = from bbb in unitOfWork.Checklists
                     orderby bbb.class
                     select bbb;

以及方法B

        var group = (from p in unitOfWork.Checklists
                     group p by p.class into g
                     select new Checklists
                     {
                         class= g.Key,
                          name= g.ToString()
                     }).AsEnumerable();

但都失败了〜 对此有何看法?这让我头晕目眩!

2 个答案:

答案 0 :(得分:1)

尝试在visual studio C#sample

中使用ObjectDumper项目
using System;
using System.Collections.Generic;
using System.Linq;

namespace testLinq
{
    public class Record
    {
        public string ClassName {get;set;}
        public string Student {get;set;}
        public string Gender {get;set;}


    }

    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Record[] list = new Record[] { 
                new Record { ClassName="A" , Student="john" , Gender="male" },
                new Record { ClassName="B" , Student="George" , Gender="male" },
                new Record { ClassName="A" , Student="bill" , Gender="Female" },
                new Record { ClassName="A" , Student="Steve" , Gender="male" }
            };

            var result = list.GroupBy(x => x.ClassName).Select(x => new { k = x.Key, person = x});

            ObjectDumper.Write(result, 1);

            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

输出

Hello World!
k=A     person=...
  person: ClassName=A     Student=john    Gender=male
  person: ClassName=A     Student=bill    Gender=Female
  person: ClassName=A     Student=Steve   Gender=male
k=B     person=...
  person: ClassName=B     Student=George  Gender=male
Press any key to continue . . .

示例MVC代码

public class YourViewModel
{
    public YourViewModel()
    {
        Record[] list = new Record[] { 
            new Record { ClassName="A" , Student="john" , Gender="male" },
            new Record { ClassName="B" , Student="George" , Gender="male" },
            new Record { ClassName="A" , Student="bill" , Gender="Female" },
            new Record { ClassName="A" , Student="Steve" , Gender="male" }
        };

        var result = list.GroupBy(x => x.ClassName).Select(x => new { k = x.Key, person = x});
        using (var writer = new StringWriter())
        {
            ObjectDumper.Write(result, 1 , writer);
            pivotString = writer.ToString();
        }
    }

    private string pivotString;

    public string PivotTableText { get { return pivotString; } }
}

public static class YourHtmlExtension
{
    public static string EncodedMultiLineText(this HtmlHelper helper, string text) 
    {
      if (String.IsNullOrEmpty(text)) 
      {
        return String.Empty;
      }
      return Regex.Replace(helper.Encode(text), Environment.NewLine, "<br/>")
    }
}

在您的控制器中

    public ActionResult ReadPivotTable()
    {
        return View((new YourViewModel());
    }

在你看来

<%= Html.EncodedMultiLineText(Model.PivotTableText) %>

答案 1 :(得分:1)

是C#中的关键字,我不能用作属性名称。你需要一个group by,你可能需要创建一个包装类来传递结果

from p in unitOfWork.Checklists
group p by p.Class into groupByClass
select new
{
   Class= groupByClass.Key,
   Students = groupByClass
}