user2023203
user2023203

Reputation: 556

C# Linq return object

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?

Upvotes: 4

Views: 7161

Answers (7)

JMan
JMan

Reputation: 2629

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.

Upvotes: 1

gleng
gleng

Reputation: 6304

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.

Upvotes: 2

David S.
David S.

Reputation: 6105

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();

Upvotes: 0

Ric
Ric

Reputation: 13248

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

This returns a Referee object.

Upvotes: 0

oerkelens
oerkelens

Reputation: 5151

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.

Upvotes: 2

Ahmed ilyas
Ahmed ilyas

Reputation: 5832

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?

Upvotes: 0

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22094

Try

return result.FirstOrDefault();

Upvotes: 10

Related Questions