Solved

linq select clause incorrect error

Posted on 2010-09-02
11
1,312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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