Find the Non integer value from a List<T> using LINQ or Lambda C#

I have a class as :

public class MyClass
{
  public string Name{set;get;}
  public string Value {set;get;}
  public int ParentId {set;get;}
}

My Class Value property can have values like: "1", "MyValue", "2".... and so on.

I have a Generic List List<MyClass> and I have to look into the list using LAMBDA or LINQ to fetch the non numeric value i.e. "My Value". Means that I have to fetch the MyClass instance that is having "My Value" in its Value property.

Any advice will be greatly appreciated.

Jon Skeet
people
quotationmark

It sounds like you want something like:

var nonInteger = list.Where(x =>
                            {
                                int ignored;
                                return !int.TryParse(x.Value, out ignored);
                            });

Or alternatively, wrap the TryParse call in a separate method, maybe even an extension method:

public static bool CanParseToInt32(this string value)
{
    int ignored;
    return int.TryParse(value, out ignored);
}

Then:

var nonInteger = list.Where(x => !x.Value.CanParseToInt32());

people

See more on this question at Stackoverflow