嗨,如果我没有理解你的问题,你可以这样尝试
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
}
我希望这会给你一些想法。