0

What's the difference between the following two? The first 1 works but the 2nd one errors out stating that 'Select' cannot be found. I can't understand why.

1st:

Office Office = cHelper.Offices .Where(o => o.IP3rdOctet == OSHelper.Get3rdOctetOfMyIP()) .FirstOrDefault(); 

2nd:

Office Office = from o in cHelper.Offices .Where(o => o.IP3rdOctet == OSHelper.Get3rdOctetOfMyIP()) .FirstOrDefault() select o; 
6
  • 3
    Not sure why you're doing a from/select when you're already getting a firstordefault. Commented Jun 28, 2012 at 14:36
  • Not sure why are mixing different styles (query syntax vs Extension method syntax) Commented Jun 28, 2012 at 14:37
  • Both concepts compile for me using List<string> ! Commented Jun 28, 2012 at 14:39
  • @Spacebison: try to use List<Office> it wouldn't work. i think it is call normal inference type for method argument ? Commented Jun 28, 2012 at 14:45
  • 2
    @SpaceBison A string may be queried as a collection of characters. An Office may not be queried - The Office may have collections, but it is not acting as a collection. Due to this, the call to Select cannot resolve (neither Enumerable.Select nor Queryable.Select apply, and office didn't implement Select itself). Commented Jun 28, 2012 at 15:03

2 Answers 2

2

This is not an IEnumerable or IQueryable, it is an instance of Office:

cHelper.Offices.Where(o => o.IP3rdOctet == OSHelper.Get3rdOctetOfMyIP()).FirstOrDefault() 

You cannot call select on that. Remove the call to FirstOrDefault(), then you will be able to select the results (which will be empty if no items match your criteria).

If you then still need the FirstOrDefault item, then put the query in brackets and append FirstOrDefault() like this:

Office Office = (from o in cHelper.Offices where o.IP3rdOctet == OSHelper.Get3rdOctetOfMyIP() select o).FirstOrDefault(); 
Sign up to request clarification or add additional context in comments.

Comments

1

select works with an IEnumerable, you are trying to use select after FirstOrDefault

rewrite like this:

Office Office = (from o in cHelper.Offices where o.IP3rdOctet == OSHelper.Get3rdOctetOfMyIP() select o).FirstOrDefault(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.