tags:

views:

140

answers:

7

I have a IEnumerable<T> collection with Name, FullName and Address.

The Address looks like this:

Street1=54, Street2=redfer street, Pin=324234
Street1=54, Street2=fdgdgdfg street, Pin=45654
Street1=55, Street2=tryry street, Pin=65464

I want to loop through this collection and print only those Names, FullNames whose Street1=54

How can i do it in LINQ?

Ok I was able to do this to extract Street1 of the Address

coll.Address.Split(",".ToCharArray())[0]returns me Street1=54 .

Now how do I add this to the condition and print only those Name, FullName whose Street1=54

+1  A: 

Try this

var result = collection.Where(x => x.Address.Street1==54);
foreach ( var cur in result ) {
  Console.WriteLine(var.Name);
}
JaredPar
A: 

Select the correct list of :

IList<T> matches = myListOfEnumerables.Where(m => m.Street1 == 54).ToList();

Then loop and print.

Chris Holmes
If all you're going to do is loop forwards through the results once, you don't need IList, and so you don't need .ToList(). See JaredPar's answer.
Daniel Earwicker
A: 

Actually the record looks like this:

{Name="Jan" FullName="Kathy Jan" Address="Street1=54, Street2=redfer street, Pin=324234"}

I have to loop through this collection and print only those Names, FullNames whose Street1=54

Viks
A: 

If the updated information is accurate, you should change the way you store the data.

It looks like you've packed the address information into a string. Why not store it as an object. In fact, why not just as more fields in the same object as the Name and FullName? (and why duplicate the first-name information?)

public class Person
{
    public string FirstName, LastName, Street1, Street2, Pin;
}

IEnumerable<Person> persons = GetAllPersonsSomehow();

foreach (Person person in persons.Where(p => p.Street1 == "54"))
    Console.WriteLine(person.LastName + ", " + person.FirstName);

Assuming you have to keep the address information in a string, you need a parser for it.

public static IDictionary<string, string> GetAddressFields(string address)
{
    return address.Split(',').ToDictionary(
                s => s.Substring(0, s.IndexOf('=')).Trim(),
                s => s.Substring(s.IndexOf('=') + 1).Trim());
}


foreach (Person person in persons.Where(p => 
                GetAddressFields(p.Address)["Street1"] == "54")) 
    Console.WriteLine(person.LastName + ", " + person.FirstName);
Daniel Earwicker
Can I do something like this?var result = collection.Where(x => x.Address.Contains("Street1=" + street1 )).OrderByDescending(x => x.Name.Length);
Viks
+2  A: 

Based on your update, you can adapt Jared Par's code this way:

var result = collection.Where(x => x.Address.Contains("Street1=54"));
foreach ( var cur in result ) {
  Console.WriteLine(string.Format("{0}, {1}", cur.Name, cur.FullName));
}

If you want to be able to plug in your Street1 value with a variable, then do this:

var street1 = "54";
var result = collection.Where(x => x.Address.Contains("Street1=" + street1 ));
foreach ( var cur in result ) {
  Console.WriteLine(string.Format("{0}, {1}", cur.Name, cur.FullName));
}

BTW, you really should update your question or add a comment to a specific answer rather than adding a new answer that isn't.

Joel Coehoorn
Ok i will update my question henceforth
Viks
Can I do something like this?var result = collection.Where(x => x.Address.Contains("Street1=" + street1 )).OrderByDescending(x => x.Name.Length);
Viks
A: 

So, what you could do is that you could write a generator for parsing the Address field and then enumerating properties of that. This is a fairly common thing in the functional programming world.

To be fair you would want this code to be lazy in that it would only compute a minimal set. I'm gonna suggest some code from the BCL but you can (and probably should) rewrite the same helper methods with generators.

public static IEnumerable<KeyValuePair<string,string>> NameValueSplit( this string s )
{
    foreach (var x in s.Split(','))
    {
        var y = x.Split(new char[] { '=' }, 2, StringSplitOptions.None);
        yield return new KeyValuePair<string, string>(y[0].TrimStart(), y[1].TrimEnd());
    }
}

With that helper function you can write code like this

var result = collection.Where(x => x.Address
    .NameValueSplit().Any(x => x.Key == "Street1" && x.Value == "54"));
foreach ( var item in result ) 
{
    Console.WriteLine(item.Name);
}

Now this code will not run on your SQL Server if you were thinking of that, but you could write a WHERE clause where you would search the Address field for a sub string %Street1=54%. I like lazy evaluation for string operations and think that's a lacking feature in the BCL. That is why I suggested that kind of solution.

John Leidegren
this looks good. i will try and let you know
Viks
A: 

No its not my List. It comes from somewhere.

Viks
The answer to that is in my answer. By the way, you really ought to EDIT your original question. These things down here, they're called answers! What you're posting here is not an answer. :)
Daniel Earwicker