I am looking for a way to search a list that returns me an bunch of indexes based on a search i make on that list. For ex i have a comma separated string list as follows:
Blue, 3
Red, 3
Blue, 1
Blue, 9
Red, 5
I want to make a search that returns me indexes of all elements EXCEPT any that contain the text found in a criteria list. The criteria list could contain:
Blue, 3
Red, 5
So in pseudo code, it would be,开发者_如何学运维
ColorList.SelectIndex(!Containing(Words found in all elements of criteriaList)
The above should return indexes 1,2,3
Thanks
var list = new []{"Blue, 3", "Red, 3", "Blue, 1", "Blue, 9", "Red, 5"};
var criteria = new []{"Blue, 3", "Red, 5"};
var filtered = list
.Select((s,i)=>new {s,i})
.Where(e => !criteria.Contains(e.s))
.Select(e => e.i);
results: { 1, 2, 3 }
var indexes = ColorList.Select((x, i) => new { Value = x, Index = i })
.Where(x => !criteriaList.Contains(x.Value))
.Select(x => x.Index);
If your lists contain many items then you might get better performance by converting criteriaList
to a HashSet<T>
first. (You'll need to benchmark to determine whether this is the better option in your case.)
var criteriaSet = new HashSet<string>(criteriaList);
var indexes = ColorList.Select((x, i) => new { Value = x, Index = i })
.Where(x => !criteriaSet.Contains(x.Value))
.Select(x => x.Index);
void Main()
{
var data = new List<string> {"Blue, 3", "Red, 3", "Blue, 1", "Blue, 9", "Red, 5"};
var colorList = new List<string> {"Blue, 3", "Red, 5"};
var indexes = data.Except(colorList).Select (x => data.IndexOf(x));
indexes.Dump();
}
Sounds like you may want to use a
List<KeyValuePair<string, int>> pair = new KeyValuePair<string, int>
then you can do
foreach (KeyValuePair<string, int> p in pair){
s = p.Key;
i = p.Value;
}
to get the values
I am not quite sure what you want to achieve here, so perhaps my answer is not what you are looking for.
Anyway, in a generic list, you could use a lambda expression/ linq statement on your collection. Consider these examples I have written for you:
internal class ListLambdaLINQSample
{
List<KeyValuePair<Colors, int>> listSource;
List<KeyValuePair<Colors, int>> listCriteria;
List<KeyValuePair<Colors, int>> listMatches;
private const int COLORCODE1 = 1;
private const int COLORCODE2 = 2;
private const int COLORCODE3 = 3;
private const int COLORCODE4 = 4;
private const int COLORCODE5 = 5;
internal enum Colors
{
Red, Blue, Green, Yellow
}
public ListLambdaLINQSample()
{ // populate the list
listSource = new List<KeyValuePair<Colors, int>>();
listCriteria = new List<KeyValuePair<Colors, int>>();
_populateListCriteria();
_populateListSource();
...
}
private void _getMatchesWithLINQ()
{
listMatches =
(from kvpInList
in listSource
where !listCriteria.Contains(kvpInList)
select kvpInList).ToList();
}
private void _getMatchesWithLambda()
{
listMatches =
listSource.Where(kvpInList => !listCriteria.Contains(kvpInList)).ToList();
}
private void _populateListSource()
{
listSource.Add(new KeyValuePair<Colors, int>(Colors.Blue, COLORCODE1));
listSource.Add(new KeyValuePair<Colors, int>(Colors.Green, COLORCODE2));
listSource.Add(new KeyValuePair<Colors, int>(Colors.Red, COLORCODE3));
listSource.Add(new KeyValuePair<Colors, int>(Colors.Yellow, COLORCODE4));
}
private void _populateListCriteria()
{
listCriteria.Add(new KeyValuePair<Colors, int>(Colors.Blue, COLORCODE1));
listCriteria.Add(new KeyValuePair<Colors, int>(Colors.Green, COLORCODE2));
}
}
Hope this helps!!
Regards, Nico
PS: I haven't compiled nor tested this code.
精彩评论