使用MVVM的Silverlight ListBox分组 [英] Silverlight ListBox Grouping using MVVM
问题描述
在列表框中分组时出现问题.实际上我的表结构如下.
Emp_ID EmpName EmpParent_ID
1000002 Vivek 1000005
1000003罗希特1000005
1000005阿贾伊1000031
1000006 Sanjay 1000005
1000011 Hemant -1
1000022 Amit 1000005
1000024 Aakash 1000006
1000027 Ameya 1000005
1000030拉姆1000005
1000042 Arvind -1
现在,我想对EmpParent_ID进行分组.输出应如下所示:
阿贾伊
Vivek
罗希特
桑杰伊
阿米特
阿美亚
公羊
桑杰伊
阿卡什
如果使用ListBox无法做到这一点,那么请建议我以其他方式使其正常工作...
I have problem while grouping in listbox. Actually my table structure is as follows.
Emp_ID EmpName EmpParent_ID
1000002 Vivek 1000005
1000003 Rohit 1000005
1000005 Ajay 1000031
1000006 Sanjay 1000005
1000011 Hemant -1
1000022 Amit 1000005
1000024 Aakash 1000006
1000027 Ameya 1000005
1000030 Ram 1000005
1000042 Arvind -1
Now I want to group on EmpParent_ID. Output should be like this:
Ajay
Vivek
Rohit
Sanjay
Amit
Ameya
Ram
Sanjay
Aakash
If this is not possible using ListBox then please suggest me other way to work it properly...
推荐答案
这很简单,您使用的是实体框架还是存储过程或简单选择?
无论哪种方式,您都可以对数据库进行过滤
您选择的只是放这样的东西
通过EmpName从TABLENAME组中选择distinct(EmpName),Emp_ID,EmpParent_ID
或者您可以使用LINQ
例子
It is very simple, are you using Entity Framework or Storage Procedure or simple select ?
either way you can filter on database
on your select just put something like that
select distinct(EmpName),Emp_ID, EmpParent_ID from TABLENAME group by EmpName
or you can use LINQ
example
class Program
{
static void Main(string[] args)
{
List<person> pessoas = new List<person>()
{
new Person(){ id = 1, name = "fernando"},
new Person(){ id = 2, name = "rodrigo"},
new Person(){ id = 4, name = "fernando"},
new Person(){ id = 5, name = "rogerio"},
new Person(){ id = 6, name = "fernando"},
new Person(){ id = 7, name = "davi"}
};
foreach (Person pes in pessoas.Distinct(new CustomComparer()))
{
Console.WriteLine(pes.name);
}
Console.Read();
}
}
public class Person
{
public int id { get; set; }
public string name { get; set; }
}
public class CustomComparer : IEqualityComparer<person>
{
public bool Equals(Person x, Person y)
{
return x.name.Equals(y.name);
}
public int GetHashCode(Person obj)
{
return this.GetHashCode();
}
}</person></person></person>
这篇关于使用MVVM的Silverlight ListBox分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!