4

I'm trying to implement a method:

  public Referee GetRefereeById(int refereeId)
        {
            var result = from b in Referees
                         where b.PersonId.Equals(refereeId)
                         select b;
            return (Referee)result;
        }

This method is supposed to return a referee object. Any idea what I'm doing wrong?

7 Answers 7

10

Try

return result.FirstOrDefault();
Sign up to request clarification or add additional context in comments.

2 Comments

This will work but i find implementing the First(OrDefault) Method not very suited. You never know what record you are going to get returned and it seems be expecting an unique result. It should actually break if there is more then one. I tend to use FirstOrDefault only when i realy want the first based on some sortings
@Jeroen You might consider adding this info to the original question - it changes the requirements a bit. That being said, you probably want to use SingleOrDefault. This will throw an exception if multiple elements are returned by your query, and will return null if no elements are returned.
2

Your select can return more than 1 referee. Try

public Referee GetRefereeById(int refereeId)
    {
        var result = (from b in Referees
                     where b.PersonId.Equals(refereeId)
                     select b).FirstOrDefault();
        return (Referee)result;
    }

You could use .Single(), but FirstOrDefault is a safer option. I am assuming PersonId should be unique, so that shouldn't matter, but if no matching data is found, Single will throw an exception.

3 Comments

I would still prefer Single. By returning the First you have no control what object you'll be returning. If there is a method to get one Referee. Just make sure it returns the unique and correct one.
I agree that in case the query returns more than one person the application is allowed to break. But if no data is found, that might not be the best behaviour. I guess it all depends on the specifics of the application. I have the experience of data getting corrupted and havign to change Single to FirstOrDefault with error logging in order not to disturb the production environment while we cleared the data :( I became a bit allergic to Single then, but I agree that might be an overreaction :)
in that case it is SingleOrDefault. All depends on what the method is expected to do. If you really assume there should be a record available with that id you can do a Single(). If it could be Nothing do the SingleOrDefault()
2

You need to return a single referee, not many of them.

Use FirstOrDefault - Returns the first element of a sequence, or a default value if the sequence contains no elements. (MSDN: http://msdn.microsoft.com/en-us/library/bb340482%28v=vs.110%29.aspx)

public Referee GetRefereeById(int refereeId)
{
   return Referees.FirstOrDefault(r => r.PersonId.Equals(refereeId));
}

This will return null if one isn't found.

Comments

1

In case you expect to find a result you should do

Return result.Single();

If not sure it exists do and check for null:

Return result.SingleOrDefault();

I would implement the Single() methods instead of the First(). Your method return a single referee to the caller. You want to make sure you actually get one unique referee in every case. When there is more then 1 matching your criteria you are introducing some very subtile bugs in your application. Assuming you are working against the correct record but instead you could be retrieving a complete different one.

Better use the First() methods if you have a list that you order on some sort of record and really intend to get the first row based on the ordering.

Comments

0

try this:

public Referee GetRefereeById(int refereeId)
        {
            var result = from b in Referees
                         where b.PersonId == refereeId
                         select b;
            return result.FirstOrDefault();
        }

there is also no need to do the cast for returning the result.

when you say it is supposed to return the object, what does it do?

Comments

0
var result = (from b in Referees
              where b.PersonId.Equals(refereeId)
              select b).SingleOrDefault();
return result;

This returns a Referee object.

Comments

0

Your query (more specifically the Where function) can return zero results. Use Single, SingleOrDefault, First or FirstOrDefault depending on your needs.

e.g. return (Referee)result.SingleOrDefault();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.