Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

linq select clause incorrect error

Posted on 2010-09-02
11
Medium Priority
?
1,331 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 400 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 800 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 800 total points
ID: 33588384
Or you can just append a .First() to the end
0
Technology Partners: 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!

 

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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 800 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 800 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 64

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

715 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