Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6866227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:09:23+00:00 2026-05-27T03:09:23+00:00

in my Silverlight 4 application, I have an ObservableCollection which consists of objects of

  • 0

in my Silverlight 4 application, I have an ObservableCollection which consists of objects of a class and is defined by an interface:

interface myInterface()
{
  string Name { get; set; }
  string Value { get; set; }
} 

class myClass() : myInterface
{
  ...
}

ObservableCollection<myInterface> _collection;

Before adding a new element to the collection, I want to make sure, that the Name-Property does not already exists within the current collection elements.
As I cannot work with contains, I currently iterate through all elements and check each element manually.

private bool CollectionContainsElement(string name2CheckAgainst)
{
  foreach (myInterface item in _collection)
    if (item.Name.Equals(name2CheckAgainst))
      return true;

  return false;
}

I have read that this can also be achieved via a Lambda Expression, so I wrote the following:

if (!_collection.Contains(p => p.Name == name2CheckAgainst))
{
  ...

But now I get an error, saying that the “lambda expression could not be converted to the Type “myInterface”, because it is no delegate-type”. (Wording may differ, as I translated it from the german version)

I’m not sure what I have to change to make it work. using System.Linq; is included. And the second question (or maybe the primary question): I have read, that the runtime changes from O(1) for the Contains()-method to O(n) – which isn’t faster than my current check. So does it even make sense to change it to using the lambda? And finally, is there probably another method in checking for an existing Name-Property in my class?

Thanks in advance,
Frank

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:09:24+00:00Added an answer on May 27, 2026 at 3:09 am
    1. You don’t have to write a Contains method, the Any method of Linq is already doing that:

      if (!_collection.Any(p => p.Name == name2CheckAgainst))
      
    2. If you want to use a Lambda, you have to change the prototype of your Contains method to accept a Lambda (a lambda is just an alternative way to write an anonymous function):

      private bool CollectionContainsElement(Func<myInterface, bool> lambda)
      {
        foreach (myInterface item in _collection)
          if (lambda(item))
            return true;
      
        return false;
      }
      
    3. Using a lambda here doesn’t change the complexity of your function, it’s O(n) in both case. So it’s just a matter of preference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Silverlight application that is built from a set of Silverlight class
I have a Silverlight application in which I would like to call a WCF
I have a Silverlight application which has on it a Canvas. On that Canvas,
I have a silverlight application which reads data from a db and displays them
in my Silverlight 4 Application I have an ObservableCollection that I data bind to
in my Silverlight 4 Application, I have an observable list of a user defined
I am developing a Silverlight application which has a list <ListBox x:Name=_list_collection SelectionChanged=SelectionChanged ScrollViewer.VerticalScrollBarVisibility=Auto
I have silverlight application in asp.net web. In web application i have folder which
I have a silverlight application, which requires charts with printing. As silverlight does not
I have Silverlight application which connects to a WCF service. If i open the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.