-3

I have a method that looks like this:

public static string MyMethod(string myParameter)
{
    var defaultProperty = new Validation() {IDNumber = "ID Number Not Found", Logon = "ID Number Not Found" };
    try
    {
        return lstLogons.DefaultIfEmpty(defaultProperty).Single(x => x.IDNumber == myParameter).Logon;
    }
    catch (Exception exception)
    {
        throw new ArgumentException(exception.Message, myParameter);
    }
}

When testing, I am giving myParameter a value that I know doesn't exist, so I want to be able to give a default value for these types of situations. But, instead it just throws an exception:

Sequence contains no matching element

I know it doesn't contain the element I am searching for.. hence the need/want for a default value.

How can I make this work?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • Unless `myParameter` is `"ID Number Not Found"`, that exception is logical. – CodeCaster Feb 06 '18 at 13:49
  • please provide more code. theres not enough information. it is your `Single` call however. – Daniel A. White Feb 06 '18 at 13:49
  • @DanielA.White that's literally all of my code dealing with this problem.. Basically, if I took `DefaultIfEmpty` out of the lambda.. and just had `lstLogons.Single(x => x.IDNumber == myParameter).Logon;`.. I don't want exceptions thrown if `myParameter` cannot be found.. I just want a default value. – Grizzly Feb 06 '18 at 13:52
  • Your `defaultProperty` does not have an `IDNumber` which will match `myParameter`, hence `.Single()` throws. – Matthew Watson Feb 06 '18 at 13:52
  • whats `lstLogons`? how does that related to a "defaultProperty". – Daniel A. White Feb 06 '18 at 13:54
  • @DanielA.White `lstLogons` is a list of `Validation` objects – Grizzly Feb 06 '18 at 13:54

1 Answers1

2

It is because you are calling Single() after that, and DefaultIfEmpty() will return collection with just one item in it and calling Single() means that there will always be always one item in it with the criteria you specified and it didn't matched, what you need hereSingleOrDefault() which will not throw exception if no matching item found, insead it will return null.

I want to return a default

you can create a local variable for that with default value:

var logon = String.Empty;

var result =  lstLogons.SingleOrDefault(x => x.IDNumber == myParameter);
if(result!=null)
    logon = result.Logon;

return logon;
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • " and DefaultIfEmpty() will return collection with no items in it" - no it won't. It'll return a collection with a single element (the default value for the type) if the source is empty... hence the name. However, the overload of `Single` accepting a predicate won'tt match that... – Jon Skeet Feb 06 '18 at 13:58
  • @JonSkeet my bad, thanks for the correction, updated to reflect that :) – Ehsan Sajjad Feb 06 '18 at 14:00
  • When running this, is there even a purpose to use the `defaultProperty` when using `SingleOrDefault`? If `SingleOrDefault` returns a null value without the use of `DefaultIfEmpty`.. then there should be no use for creating `defaultProperty`? – Grizzly Feb 06 '18 at 14:10
  • valid point @GTown-Coder adjusted the code – Ehsan Sajjad Feb 06 '18 at 14:13
  • What if I just did `return lstLogons.SingleOrDefault(x => x.IDNumber == myParameter).Logon`? That should either return `null` or an actual `Logon`? What's the purpose of creating another object, (`logon`), when I could just return the value in 1 line? – Grizzly Feb 06 '18 at 14:16
  • if `SingleOrDefault` returns `null` then accessing `Logon` would cause run-time error @GTown-Coder and we are not micro-optimizing the code for OP, the purpose is to outline the issue and provide the working solution. – Ehsan Sajjad Feb 06 '18 at 14:20
  • 1
    Ahhh understood. I was thinking of it as just one whole line being executed simultaneously.. and wasn't thinking that `SingleOrDefault` is ran first and then the `Logon` is retrieved. Early morning.. need more caffeine. Thank you for your assistance. Very much appreciated. – Grizzly Feb 06 '18 at 14:22
  • 1
    no worries, take a cup of coffee ;) glad to help @GTown-Coder – Ehsan Sajjad Feb 06 '18 at 14:27