Samantha J T Star
Samantha J T Star

Reputation: 32758

Manipulating data in a collection with LINQ

I use the following:

        var menuItems = _menuRepository.GetPk(pk)
            .Where(m => m.Status == "00")
            .OrderBy(m => m.Order)
            .Select(m => new MenuItem
            {
                PartitionKey = m.PartitionKey,
                RowKey = m.RowKey,
                Order = m.Order,
                Link = m.Link
            });

To retrieve a collection of strings such as these below. All strings are the same length (8 columns) and are sorted in order:

0-0-0-00
0-1-0-00
0-2-0-00
0-3-0-00
1-0-0-00
1-1-0-00
1-1-1-00
1-1-2-00
1-1-3-00
1-2-0-00
3-1-0-00
3-2-0-00

What I need to do is if given a string such as "1-1-1-00" then I need to retrieve all the rows from the menuItems list, where the first four columns match the input string except the one row where there is a zero in the 5th column. Such as:

1-1-1-00
1-1-2-00
1-1-3-00 

Upvotes: 0

Views: 228

Answers (2)

Sagi
Sagi

Reputation: 1612

String input = "1-1-1-00";
var wantedStrings = stringList.Where(str => str.StartsWith(input.Substring(0, 4)) && str[4] != '0');

Upvotes: 3

Amen Ayach
Amen Ayach

Reputation: 4348

string given = "1-1-1-00";
string sptGiven = given.Split('-');
var result = stringList.Where(m => m.Split('-')[0] == sptGiven[0] && 
                              m.Split('-')[2] == sptGiven[2] && 
                              m.Split('-')[3] == sptGiven[3]);

Upvotes: 2

Related Questions