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 tr开发者_JS百科ied 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!
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) %>
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
}
精彩评论