过滤一列数据的重复项,但显示另一列的所有数据

发布于 2024-12-09 20:16:58 字数 852 浏览 0 评论 0原文

我正在做 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();

,但都失败了~ 有什么想法吗?这让我很头疼!

i am doing mvc3 and linQ
i have the database like this

class    student        gender

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

how do i code to filter the duplicate as the result as below

A
John       male
bill       female
steve      male

B
George     male

i have tried method A

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

and also method 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();

but all failed~
Any idea for this ? This is breaking my head!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2024-12-16 20:16:58

尝试在 Visual Studio C# 示例中使用 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) %>

try to use ObjectDumper project in the visual studio C# sample

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);
        }
    }
}

The output is

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 . . .

Example MVC code

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/>")
    }
}

In your controller

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

In your view

<%= Html.EncodedMultiLineText(Model.PivotTableText) %>
∞觅青森が 2024-12-16 20:16:58

class 是 C# 中的关键字,不能用作属性名称。您需要一个分组依据,并且可能需要创建一个包装类来传递结果

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

class is a keyword in C# and I you can't use as property names. You need a group by and probably you need either to create a wrapper class to pass the result

from p in unitOfWork.Checklists
group p by p.Class into groupByClass
select new
{
   Class= groupByClass.Key,
   Students = groupByClass
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文