Solved

linq select clause incorrect error

Posted on 2010-09-02
11
1,297 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
ID: 33588192

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
ID: 33588360
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
ID: 33588384
Or you can just append a .First() to the end
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

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

Author Comment

by:ToString1
ID: 33588692
Ok thanks folks; I'm on the move but will award point tomorrow mornng
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 total points
ID: 33591572
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
ID: 33593883
Thanks guys;  I will update later today with points
0
 

Author Comment

by:ToString1
ID: 33594590
Thanks folks - very interesting.

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

Author Comment

by:ToString1
ID: 33594994
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 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 total points
ID: 33596240
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 63

Expert Comment

by:Fernando Soto
ID: 33596303
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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