Karloss
Karloss

Reputation: 817

LINQ to XML query and filtering names

I am working at Windows Phone 7 C#, XAML, XML and LINQ programming. I need to organize search by part of the name at following XML:

<Row>
  <Myday>23</Myday>
  <Mymonth>12</Mymonth>
  <Mynames>Alex, Joanna, Jim</Mynames>
</Row>

<Row>
  <Myday>24</Myday>
  <Mymonth>12</Mymonth>
  <Mynames>John, David</Mynames>
</Row>

I have following query:

var myData = from query in loadedData.Descendants("Row")
             where query.Element("Mynames").Value.Contains("Jo")
             select new Kalendars
                        {
                            Myday = (int)query.Element("Myday"),
                            Mymonth = (int)query.Element("Mymonth"),
                            Mynames = (string)query.Element("Mynames")
                        };
             listBoxSearch.ItemsSource = myData;

Query problem is, that it will return full part of the names like "Alex, Joanna, Jim" and "John, David". How can I get only Joanna and John? Second question is how it is possible to do that user enters ...Value.Contains("jo") and query still returns Joanna and John?

Upvotes: 0

Views: 561

Answers (1)

Jeff Mercado
Jeff Mercado

Reputation: 134521

You'll need to break apart that string to get the individual names. In this case, something like this will be fine.

string line = "Alex, Joanna, Jim";
string[] names = line.Split(new[] { ", " }, StringSplitOptions.None);
var jos = from name in names
          where name.Contains("Jo")
          select name; // ["Joanna"]

Just work this into your query.

The original string contains the letters "Jo" so calling that function returns true. It doesn't look at each of the words individually, just the string as a whole.

Upvotes: 1

Related Questions