views:

112

answers:

8

I have a list of objects, called Attributes, that, essentially, i need to do the following in C#

<pseudocode>
if (list.Contains(Attribute where getName() == "owner"))
{
   do stuff
}
</pseudocode>

the problem I'm having is the nested bracket bit of the if - "Attribute where getName() == "owner". This is my code - it doesn't work, obviously, but most of the if should be right, it's just getting that i need to do the bit in forward slashes and i don't know how.

if (attributes.Contains(Attribute /where/ attribute.getName() == "Owner"))
    {
        string value = attr.getValue();
        value = value.Replace(domain, "");
        user = value;
        UserExists(value);
    }

I'm probably being dense, but I had to restart 3 days development to change everything to using Attribute objects, so my brain is rather destroyed. Sorry.

+1  A: 

You can use LINQ to objects

if (attributes.Count(a => a.getName() == "Owner") > 0)
Rob Windsor
Please use `attributes.Any` instead of `attributes.Count` because count loops through the whole collection and Any just returns true when one attribute is found -> increases performance
Zenuka
@Rob - `Count()` shouldn't be used if you're testing if it has *at least one*. Instead, `Any()` should be used as this will move the enumerator to the first element (`Count()` will potentially iterate through all of them).
TheCloudlessSky
The comments above are correct - my oversight
Rob Windsor
+10  A: 

If you are using a version of .NET that supports LINQ, try

if(attributes.Any(attribute=>attribute.getName()=="Owner")
murgatroid99
+2  A: 
if(list.Exists(e=>e.getName() == "owner")) {
   //yup
}
nos
A: 

are you using .NET 3.5 or above, if so and presuming that 'attributes' is derived from IEnumerable, you could do the following :

if (attributes.Any(attrib=>attrib.GetName() == "Owner"))
{
    //Do code here...
}
Richard Friend
A: 

Please have a look:

var filteredAttributes = from attribute in Attributes 
                         where string.Compare(attribute.getName() ,"Owner",true)==0
                         select attribute;

foreach(var attribute in filteredAttributes)
{
          string value = attr.getValue();
                value = value.Replace(domain, "");
                user = value;
                UserExists(value);

}
DrakeVN
A: 

Ok, i worked it out, I found a much better way to do it:

for (int i = 0; i < attributes.Count; i++)
{
    if (attributes[i].getName() == "Owner")
    {
       string value = attributes[i].getValue();
       value = value.Replace(domain, "");
       user = value;
       UserExists(value);
    }
}

which makes more sense, and actually works.

omicronlyrae
This is not doing what you originally stated, this will action every attribute in the list that matches "Owner", your original pseudo code only tested for the condtion, then actioned some code if that condition was met...
Richard Friend
It just needs a break; after UserExists(value);
Eric Mickelsen
Even with the break, the code seems less clear with a loop than with an existance test in an if statement
murgatroid99
I realise it's a rather awful way to do what I want, but I'm far too new to C#, and in honesty, programming in general, to know, well, anything. This gets me the result I want, and it works, and i understand it, while other methods on this page, which i have tried, I don't understand how to tie it in and get what i need. I hate being a noob. =(
omicronlyrae
A: 

You can use LINQ to separate out the required attributes...

IEnumerable<TAttribute> ownerAttributes =
    attributes.Where(attribute => attribute.getName() == "Owner");

... and then iterate over those attributes, applying the relevant logic...

foreach(TAttribute attribute in ownerAttributes)
{
    // Do stuff...
}
m_arnell
+2  A: 

Without LINQ!:

if (list.Exists(delegate(Attribute a) { return a.GetName() == "Owner"; }))
Eric Mickelsen