[英]How to compare column values of list
I have a list of rows having many columns. 我有一个包含许多列的行列表。 Say, a song is a row, and album,artist,title,year are it's columns.
假设一首歌是连续的,而专辑,艺术家,标题,年份则是专栏。 These are displayed in a list view.
这些显示在列表视图中。 When i multi-select, say 5 songs, i need to check if the artist is the same for all 5 songs or not.
当我多选,比如说5首歌曲时,我需要检查艺术家是否对所有5首歌曲都相同。 The same for album,title,year.
专辑,标题,年份相同。
my pseudo-code is as follows: 我的伪代码如下:
ListView.SelectedListViewItemCollection selectedItem = this.lvuFiles.SelectedItems;
foreach (ListViewItem itemSelected in selectedItem)
{
// pass a list of 'title's to a method to check for equality
}
i have implemented the method which checks for equality. 我已经实现了检查相等性的方法。 I just could not figure out how to store the lists (title list,album list, etc) and pass them.
我只是不知道如何存储列表(标题列表,相册列表等)并通过它们。
EDIT: 编辑:
Screenshot of List View http://i.min.us/iexmSg.png 列表视图的屏幕快照http://i.min.us/iexmSg.png
now, i need to check the 3 Titles are matching or not. 现在,我需要检查3个标题是否匹配。 One way i thought was to add all the titles to a list and pass to the method.
我认为的一种方法是将所有标题添加到列表中并传递给该方法。 Do the same for artist,album.
对艺术家,专辑做同样的事情。 I could not get the selectedItems' value.
我无法获取selectedItems的值。 SO, i have to maintain multiple lists for multiple properties.
所以,我必须维护多个列表的多个属性。 Is there any better way.
有没有更好的办法。 As for the suggestions given, i tried the linq one.
至于给出的建议,我尝试了linq。 Says, method not defined.
说,方法未定义。
Is there any better way.
有没有更好的办法。
Absolutely. 绝对。 The LINQ suggestion, for starters, is perfect;
LINQ建议对于初学者来说是完美的; the only problem is that the type of the
ListView.SelectedItems
property does not implement IEnumerable<ListViewItem>
; 唯一的问题是
ListView.SelectedItems
属性的类型未实现IEnumerable<ListViewItem>
; I'm guessing that's why it didn't work for you. 我猜这就是为什么它对您不起作用的原因。 You can fix this with a simple
Cast<T>
call: 您可以通过简单的
Cast<T>
调用来解决此问题:
if (listView.SelectedItems.Count > 0)
{
var titles = from x in listView.SelectedItems.Cast<ListViewItem>()
select x.SubItems[titleColumn.Index].Text;
string firstTitle = titles.First();
bool allSameTitle = titles.All(t => t == firstTitle);
}
Now, if the real reason the LINQ suggestion didn't work for you is that you're stuck on .NET 2.0, fear not. 现在,如果LINQ建议对您不起作用的真正原因是您停留在.NET 2.0上,请不要担心。 You can still genericize this behavior.
您仍然可以泛化此行为。 Define a method to look at each item in an
IEnumerable
and, based on some selector function, determine whether they all meet a given criterion. 定义一种方法来查看
IEnumerable
中的每个项目,并基于某些选择器功能确定它们是否均满足给定条件。 Here's an example of how you might do it: 这是您可能如何做的一个例子:
public static bool All(IEnumerable source, Predicate<object> criterion)
{
foreach (object item in source)
{
if (!criterion(item))
{
return false;
}
}
return true;
}
Then you would call this method like this on your ListView.SelectedItems
: 然后,您可以在
ListView.SelectedItems
上这样调用此方法:
if (listView.SelectedItems.Count > 0)
{
string firstTitle = listView.SelectedItems[0].SubItems[titleColumn.Index].Text;
Predicate<object> sameTitle = delegate(object obj)
{
if (!(obj is ListViewItem))
{
return false;
}
return ((ListViewItem)obj).SubItems[titleColumn.Index].Text == firstTitle;
};
bool allSameTitle = All(listView.SelectedItems, sameTitle);
}
You could use the linq functional set operator 'All'. 您可以使用linq功能集运算符“全部”。
using System.Linq;
...
var title = selectedItems.First().Column["Title"].Value;
var sameTitle = selectedItems.All(i => i.Column["Title"].Value == title);
// repeat for artist, album, year, etc.
I don't know the exact syntax of how to get a hold of the values but hopefully you can translate it. 我不知道如何获取这些值的确切语法,但希望您可以进行翻译。 I think the key is to just note that what you're essentially asking is "are all values the same".
我认为关键是要注意您本质上要问的是“所有值都相同”。 Link is your friend for such set operations.
Link是您进行此类设置操作的朋友。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.