Solved

linq select clause incorrect error

Posted on 2010-09-02
11
1,273 Views
Last Modified: 2013-11-11
On this linq query

return (from q in db.foo.Where(f => f.ID >= id).OrderBy(f => f.ID).Skip(1).Take(1);

Why do I get the error


The type of the expression in the select clause is incorrect.  Type inference failed in the call to 'Select'.      
0
Comment
Question by:ToString1
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 16

Accepted Solution

by:
kris_per earned 100 total points
Comment Utility

You need to add 'select' to the query like:

return (from q in db.foo.Where(f => f.ID >= id).OrderBy(f => f.ID).Skip(1).Take(1) select q);

OR use it like:

return db.foo.Where(f => f.ID >= id).OrderBy(f => f.ID).Skip(1).Take(1);
0
 
LVL 21

Assisted Solution

by:naspinski
naspinski earned 200 total points
Comment Utility
Just replace Take(1) with .First()

Take returns a collection, even though in this case it us just a collection of 1
0
 
LVL 21

Assisted Solution

by:naspinski
naspinski earned 200 total points
Comment Utility
Or you can just append a .First() to the end
0
 

Author Comment

by:ToString1
Comment Utility
Ok thanks folks; I'm on the move but will award point tomorrow mornng
0
 

Author Comment

by:ToString1
Comment Utility
Ok thanks folks; I'm on the move but will award point tomorrow mornng
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 total points
Comment Utility
Hi ToString1;

In you last post on this question you gave the example as follows :

FOO first = dataContext.Foos.First();
FOO last = dataContext.FOOs.Last();

And wanted something like :

FOO nextFoo = dataContext.FOOs  //get the next foo object

In Linq there are two forms of a query, (1) Method syntax as your examples show above. So I gave you a solution in the same query type, Method syntax. (2) Query syntax which starts with, "from XX in DataContext.Table" and MUST end with a "select or group clause".

You can intermix the two types in one query but you need to be careful to make sure that all Query syntax queries end in a select or group clause.

Now may I suggest that you use this query

return dataContext.Foos.Where(f => f.ID > id).OrderBy(f => f.ID).FirstOrDefault();

Which will sent a query to the server something like this:

SELECT TOP (1) [t0].[ID], ... "All other columns"
FROM [dbo].[Foos] AS [t0]
WHERE [t0].[ID] > @p0
ORDER BY [t0].[ID]
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [4]

Where a query like this one :

return dataContext.Foos.Where(f => f.ID >= id).OrderBy(f => f.ID).Skip(1).First();

Which will sent a query to the server something like this:

SELECT TOP (1) [t0].[ID], ... "All other columns"
FROM (
    SELECT TOP (1) [t0].[ID], ... "All other columns"
    FROM (
        SELECT ROW_NUMBER() OVER (ORDER BY [t0].[ID]) AS [ROW_NUMBER], [t0].[ID],
          ... "All other columns"
        FROM [dbo].[Foos] AS [t0]
        WHERE [t0].[ID] >= @p0
        ) AS [t1]
    WHERE [t1].[ROW_NUMBER] > @p1
    ) AS [t2]
ORDER BY [t2].[ROW_NUMBER]
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [4]
-- @p1: Input Int (Size = -1; Prec = 0; Scale = 0) [1]

Also the query I have suggested uses the FirstOrDefault() which if no record is found will return a null and will not throw an exception because of it.

Fernando
0
 

Author Comment

by:ToString1
Comment Utility
Thanks guys;  I will update later today with points
0
 

Author Comment

by:ToString1
Comment Utility
Thanks folks - very interesting.

I am still unsure about the skip/take versus FirstOrDefault  ?
0
 

Author Comment

by:ToString1
Comment Utility
Hi folks If I put

return dataContext.Foos.Where(f => f.ID > id).OrderBy(f => f.ID).FirstOrDefault();


Could not find an implementation of the query pattern for source type .....  'Select' not found
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 total points
Comment Utility
Hi ToString1;

To your question, "I am still unsure about the skip/take versus FirstOrDefault  ?", The Skip(X) where X is an integer skips X records from the current position in the collection. The Take(X) where X is an integer takes / returns X records from the current position in the collection. FirstOrDefault returns the first record in the collection and if the query does not produce any results it returns the default value for the data type to be returned, in this case a null value.

Fernando
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
To your question about this statement :

return dataContext.Foos.Where(f => f.ID > id).OrderBy(f => f.ID).FirstOrDefault();

The dataContext is a DBML model class that maps to the database table? Because the error states that the source does implement a Select does not make sense seeming no Select is being used.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now