列表中的类列 [英] class column in a list

查看:60
本文介绍了列表中的类列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i有一个列表,我从一个如下所示的数据库中提取:

 id name firstname adress age classby order 
1 Roger Doe Montana 45名称ASC
2 Rambo john Miami 50名称ASC
3 Doe john Montana 45名称ASC



我想从我的程序中得到的是它查看classby字段并根据order字段中的值对其进行排序。在这个例子中,我想按名称升序排序。

  id name firstname adress age classby order  
3 Doe john Montana 45名称ASC
2 Rambo john Miami 50名称ASC
1 Roger Doe Montana 45名称ASC

如果我做这样的事情会有效吗? :

  foreach (列表l  in  biglist)
{
if (l.classby == value)&& (l.order == ASC // 值在此示例中获取名称
{
biglist.OrderBy(list = > ; l.name);
}
else if ((l.classby == value)&&(l.order == DESC
{
biglist.OrderByDescending(list = > l.name);
}
}

解决方案

你可以 - 但还有其他两种方式可能会更好。第一种是按照你想要的顺序读回数据 - 这是在你的select语句中添加ORDER BY子句的简单问题:

  SELECT  *  FROM  myTable  WHERE  ...  ORDER   BY  name  ASC  

这样,数据就会按照你想要的方式排序甚至可以看到它。



另一种选择是对你正在使用的显示控件进行排序。例如,对于DataGridView:

 myDataGridView.Sort(myDataGridView.Columns [ 名称],ListSortDirection.Ascending); 





或者你可以按照自己的方式行事,除了你使用的任何OrderBy方法都没有修改原始输入数据 - 它们都返回一个已排序的IEnumerable:

  var  sorted = biglist.OrderBy(item = >  item.name); 


< blockquote>有几种方法可以实现你所追求的目标。首先,你必须意识到你有相当多的冗余 - 你在每个项目上重复ClassBy和Order字段,即使你只需要它们一次。毕竟,如果您尝试在整个列表中更改排序顺序/列,则行为将非常难以预测。现在,假设您将数据展平为类结构,您可以使用Dynamic Linq或反射来管理排序。使用DLINQ,您的查询可能如下所示:

 MyDetailsList list =  new  MyDetailsList(); 
list.Populate();
var firstItem = list.First();
var outputList = null ;
if string .Compare(firstItem.OrderBy, asc,StringComparison.InvariantCultureIgnoreCase)== 0
{
outputList = list.OrderBy(firstItem.ClassBy);
}
else
{
outputList = list.OrderByDescending(firstItem.ClassBy);
}

现在,如果你不想使用DLINQ,你可以使用类似的东西:

 MyDetailsList list =  new  MyDetailsList(); 
list.Populate();
var firstItem = list.First();
var outputList = null ;
var propInfo = list.GetType()。GetProperty(firstItem.ClassBy);
if string .Compare(firstItem.OrderBy, asc,StringComparison.InvariantCultureIgnoreCase)== 0
{
outputList = list.OrderBy(x = > propInfo.GetValue(x, null ));
}
else
{
outputList = list.OrderByDescending(x = > propInfo.GetValue(x, null ));
}


hi all,
i have a list that I extract from a database that looks like this:

id name   firstname adress   age classby order
1  Roger  Doe        Montana  45  name   ASC
2  Rambo  john       Miami    50  name   ASC
3  Doe    john       Montana  45  name   ASC


What I want from my program is that it looks in the "classby" field and orders it based on the value in the "order" field. In this example, I want to sort by name ascending.

id name firstname adress age classby order
3  Doe    john    Montana 45  name    ASC
2  Rambo  john    Miami   50  name    ASC
1  Roger  Doe     Montana 45  name    ASC

If i do something like this it will work? :

foreach (List l in biglist)
{
  if (l.classby =="value") && (l.order == "ASC") //value gets name in this exemple
  {
    biglist.OrderBy(list => l.name);
  }
  else if ((l.classby =="value") && (l.order == "DESC") 
  {
    biglist.OrderByDescending(list => l.name);
  }
}

解决方案

You could - but there are two other ways which might be better. The first is to read the data back in teh order you want - that is a simple matter of adding an ORDER BY clause to your select statement:

SELECT * FROM myTable WHERE ... ORDER BY name ASC

That way the data is ordered the way you want it before you even see it.

The other alternative is to apply a sort to the display control you are using. For example, for a DataGridView:

myDataGridView.Sort(myDataGridView.Columns["Name"],ListSortDirection.Ascending);



Or you could do it your way, except that neither of the OrderBy methods you are using modify the original input data - they both return a sorted IEnumerable instead:

var sorted = biglist.OrderBy(item => item.name);


There are a couple of ways that you can achieve what you're after. First of all, you must realise that you have a fair bit of redundancy in there - you're repeating the ClassBy and Order fields on each item, even though you only need them once. After all, the behaviour would be wildly unpredictable if you were to try to change the sort order/column, throughout the list. Now, supposing that you flatten the data out into a class structure, you can use either Dynamic Linq or reflection to manage the sort. With DLINQ, your query might look something like this:

MyDetailsList list = new MyDetailsList();
list.Populate();
var firstItem = list.First();
var outputList = null;
if (string.Compare(firstItem.OrderBy, "asc", StringComparison.InvariantCultureIgnoreCase) == 0)
{
  outputList = list.OrderBy(firstItem.ClassBy);
}
else
{
  outputList = list.OrderByDescending(firstItem.ClassBy);
}

Now, if you don't want to use DLINQ, you'd use something like this instead:

MyDetailsList list = new MyDetailsList();
list.Populate();
var firstItem = list.First();
var outputList = null;
var propInfo = list.GetType().GetProperty(firstItem.ClassBy);
if (string.Compare(firstItem.OrderBy, "asc", StringComparison.InvariantCultureIgnoreCase) == 0)
{
  outputList = list.OrderBy(x => propInfo.GetValue(x, null));
}
else
{
  outputList = list.OrderByDescending(x => propInfo.GetValue(x, null));
}


这篇关于列表中的类列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆