嗨,如果我沒有理解你的問題錯了,你可以試試它是這樣的
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
//Ignore above code
List<Table> list = new List<Table>();
//Suppose this what you db returns
list.Add(new Table() { TownName = "London", HouseType = HouseType.Flat });
list.Add(new Table() { TownName = "London", HouseType = HouseType.Flat });
list.Add(new Table() { TownName = "London", HouseType = HouseType.House });
list.Add(new Table() { TownName = "Paris", HouseType = HouseType.Flat });
list.Add(new Table() { TownName = "Paris", HouseType = HouseType.Flat });
list.Add(new Table() { TownName = "Paris", HouseType = HouseType.House });
list.Add(new Table() { TownName = "Paris", HouseType = HouseType.House });
var result = list.GroupBy(o => o.TownName).Select(s => new BrowseModel() { TownName = s.First().TownName, FlatCount = s.Where(f => f.HouseType == HouseType.Flat).Count(), HouseCount = s.Where(h => h.HouseType == HouseType.House).Count() }).ToList();
}
}
public class BrowseModel
{
public string TownName { get; set; }
public int FlatCount { get; set; }
public int HouseCount { get; set; }
}
public class Table
{
public string TownName { get; set; }
public HouseType HouseType { get; set; }
}
public enum HouseType
{
House=0,
Flat=1
}
我希望這會給你想法。
是您轉換爲BrowseModel的字符串列表類型 – ethicallogics 2013-02-13 02:21:28
它是一個對象。想象一下房子的對象。平和房子是房子類型。 – Tun 2013-02-13 10:08:24