Solved

linq select clause incorrect error

Posted on 2010-09-02
11
1,285 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 62

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 62

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 62

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 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

24 Experts available now in Live!

Get 1:1 Help Now