1

I want to make a query that returns a specific person (by ID) from person-list (persons).

 public Person getThisID(int pID)
 {
     var res = from p in persons
                         where p.pID == pID
                         select p;
     return res;
 }

But I got an error about casting problems. I tried to cast res to Person but it doesn't work with that. How can I solve it?

Abbas
  • 14,186
  • 6
  • 41
  • 72
user2097810
  • 735
  • 6
  • 17
  • 25

10 Answers10

7
var res= (from p in persons
                         where p.pID == pID
                         select p).SingleOrDefault();
            return res;
marko
  • 10,684
  • 17
  • 71
  • 92
  • Hi , I tried all the versions you gave here. with lambda it's worked. but I have to use LINQ. I still get an error: 'cannot assign method group to an implicity-typed local variable' – user2097810 Nov 06 '13 at 12:25
  • Did you keep the parentheses after `SingleOrDefault`, @user2097810? – GSerg Nov 06 '13 at 13:17
6

select p; could potentially return multiple results.

Wrap your query with a .FirstOrDefault();

 var res = (from p in persons
            where p.pID==pID
            select p).FirstOrDefault();
 return res;

This will return a null object if no user is found, or the first user with that Id if many are found.

Alternatively you could use SingleOrDefault() which will throw an exception if more than one result is found.

Both the SingleOrDefault() and FirstOrDefault() methods support passing a filter to them so you could also simplify your query into a 1 line Lambda Statement.

return persons.FirstOrDefault(p => p.pID == pID);
Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
3

Assuming Persons is a Person collection, res is an IEnumerable<Person> and not a single person. If you expect only one result, use the Single method.

dee-see
  • 23,668
  • 5
  • 58
  • 91
  • Single would throw an exception if there were no matching elements in the sequence. I'd suggest SingleOrDefault might be better here, allowing for null checking on the result. This obviously assumes the type you are selecting is nullable. – pwdst Nov 06 '13 at 12:26
2

That query will return an IEnumerable<Person> and not a single Person. That is because the where could return multiple results. Add FirstOrDefault() to your result and you're good:

public Person getThisID(int pID)
{
    var res = from p in persons where p.pID == pID select p;
    return res.FirstOrDefault();
}

This will return the first instance that meets the condition otherwise the default, most likely null.

To shorten things up you could also use a LinQ method with a lambda expression:

return persons.FirstOrDefault(p => p.pID == pID);
//or...
return persons.Single(p => p.pID == pID);
Abbas
  • 14,186
  • 6
  • 41
  • 72
  • 1
    i think that SingleOrDefault() is more suitable here – happygilmore Nov 06 '13 at 12:18
  • You're right, I prefer that method too. Just giving him the opportunities. :) – Abbas Nov 06 '13 at 12:21
  • 1
    For reference for the OP, SingleOrDefault() will throw an exception if it matches more than one entity whereas FirstOrDefault() will return the first found regardless of the number present. See http://msdn.microsoft.com/en-us/library/bb342451(v=vs.110).aspx – pwdst Nov 06 '13 at 12:23
1

if person is List then

var data = persons.Where( x => x.PID == pid).SingleOrDefault();  
return data 
Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
  • This isn't right. Use of select is wrong and should be `==` not `=` – Sam Nov 06 '13 at 13:10
  • You should change `Select` to `Where` as at the moment you are selecting `IEnumerable` if you use `persons.Select( x => x.PID == pid)` – Sam Nov 06 '13 at 13:15
1

Using only FirstOrDefault:

return persons.FirstOrDefault(p=> p.ID == pId);
danielrozo
  • 1,442
  • 1
  • 10
  • 22
0

Try this

 public Person getThisID(int pID)
        {
            Person res= (from p in persons
                         where p.pID==pID
                         select p).First();
            return res;
        }

Should work :)

Mrinal Saurabh
  • 948
  • 11
  • 16
  • 2
    The type parameter `` is superfluous. – Rik Nov 06 '13 at 12:14
  • 1
    This will also throw an exception if there is no matching Person. Either FirstOrDefault or SingleOrDefault are generally better. See http://msdn.microsoft.com/en-us/library/bb291976(v=vs.110).aspx – pwdst Nov 06 '13 at 12:25
0

you can use following snippet

 persons.FirstOrDefault(p => p.id == pID);
Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
dcoder
  • 11
  • 1
0

Very easy:

public Person getThisID(int pID)
{
    return persons.FirstOrDefault(p=> p.pID == pID);
}
-1

Please use FirstOrDefault() when returning single value

    var res= from p in persons
                 where p.pID==pID
                 select p.FirstOrDefault();  

Linq FirstOrDefault

Community
  • 1
  • 1
taha ahmed
  • 19
  • 6
  • This is a syntax error. You need to wrap the LINQ expression in parentheses then call the extension method. – ShooShoSha Dec 21 '16 at 21:29