Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlException: "No column name was specified for column 1 of 't'" on IQueryable.Count() with Skip and Take #30239

Closed
hvkooten opened this issue Feb 2, 2023 · 8 comments

Comments

@hvkooten
Copy link

hvkooten commented Feb 2, 2023

Description

Retrieving the number of records via Count() in a IQueryable with Skip() and Take() results in an exception:

Reproduction Steps

var query = context.Set<TestView>().Where(q => q.ItemType== itemType).AsQueryable();
var total = query.Count();
query =query.Skip(0).Take(1000);
var subTotal = query.Count();

Expected behavior

First Count() results in SQL statement:

exec sp_executesql N'SELECT COUNT(*)
FROM [View] AS [a]
WHERE [a].[ItemType] = @__Type_0',N'@__Type_0 nvarchar(4000)',@__Type_0=N'PO'

expected: returns rowcount

Second Count() results in SQL statement:

exec sp_executesql N'SELECT COUNT(*)
FROM (
    SELECT 1 AS column1
    FROM [View] AS [a]
    WHERE [a].[ItemType] = @__Type_0
    ORDER BY (SELECT 1)
    OFFSET @__p_1 ROWS FETCH NEXT @__p_2 ROWS ONLY
) AS [t]',N'@__Type_0 nvarchar(4000),@__p_1 int,@__p_2 int',@__Type_0=N'PO',@__p_1=0,@__p_2=1000

expected: returns 1000

Actual behavior

Second Count() results in SQL statement:

exec sp_executesql N'SELECT COUNT(*)
FROM (
    SELECT 1
    FROM [View] AS [a]
    WHERE [a].[ItemType] = @__Type_0
    ORDER BY (SELECT 1)
    OFFSET @__p_1 ROWS FETCH NEXT @__p_2 ROWS ONLY
) AS [t]',N'@__Type_0 nvarchar(4000),@__p_1 int,@__p_2 int',@__Type_0=N'PO',@__p_1=0,@__p_2=1000

and throws a sqlexception: "No column name was specified for column 1 of 't'"

Regression?

In .net 6.0 this works, but while moving to .net 7.0 we got this exception.

Known Workarounds

query.ToList().count;
But this will materialize the query and send all data to memory

Configuration

.net 7.0
development: Windows
production: Azure

Other information

The problem is, there is no column name in the query. By adding "AS column1" in the select statement the query will work.

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged label Feb 2, 2023
@hvkooten
Copy link
Author

hvkooten commented Feb 2, 2023

area-System.Linq.Expressions

@ghost
Copy link

ghost commented Feb 3, 2023

Tagging subscribers to this area: @cston
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Retrieving the number of records via Count() in a IQueryable with Skip() and Take() results in an exception:

Reproduction Steps

var query = context.Set<TestView>().Where(q => q.ItemType== itemType).AsQueryable();
var total = query.Count();
query =query.Skip(0).Take(1000);
var subTotal = query.Count();

Expected behavior

First Count() results in SQL statement:

exec sp_executesql N'SELECT COUNT(*)
FROM [View] AS [a]
WHERE [a].[ItemType] = @__Type_0',N'@__Type_0 nvarchar(4000)',@__Type_0=N'PO'

expected: returns rowcount

Second Count() results in SQL statement:

exec sp_executesql N'SELECT COUNT(*)
FROM (
    SELECT 1 AS column1
    FROM [View] AS [a]
    WHERE [a].[ItemType] = @__Type_0
    ORDER BY (SELECT 1)
    OFFSET @__p_1 ROWS FETCH NEXT @__p_2 ROWS ONLY
) AS [t]',N'@__Type_0 nvarchar(4000),@__p_1 int,@__p_2 int',@__Type_0=N'PO',@__p_1=0,@__p_2=1000

expected: returns 1000

Actual behavior

Second Count() results in SQL statement:

exec sp_executesql N'SELECT COUNT(*)
FROM (
    SELECT 1
    FROM [View] AS [a]
    WHERE [a].[ItemType] = @__Type_0
    ORDER BY (SELECT 1)
    OFFSET @__p_1 ROWS FETCH NEXT @__p_2 ROWS ONLY
) AS [t]',N'@__Type_0 nvarchar(4000),@__p_1 int,@__p_2 int',@__Type_0=N'PO',@__p_1=0,@__p_2=1000

and throws a sqlexception: "No column name was specified for column 1 of 't'"

Regression?

In .net 6.0 this works, but while moving to .net 7.0 we got this exception.

Known Workarounds

query.ToList().count;
But this will materialize the query and send all data to memory

Configuration

.net 7.0
development: Windows
production: Azure

Other information

The problem is, there is no column name in the query. By adding "AS column1" in the select statement the query will work.

Author: hvkooten
Assignees: -
Labels:

area-System.Linq.Expressions, untriaged

Milestone: -

@hvkooten
Copy link
Author

hvkooten commented Feb 8, 2023

@jaredpar @cston @333fred any update on the issue?

@cston cston self-assigned this Feb 8, 2023
@cston
Copy link
Member

cston commented Feb 8, 2023

Thanks @hvkooten, I'll investigate.

@cston
Copy link
Member

cston commented Feb 8, 2023

@ajcvickers, should this issue be assigned to area-System.Data?

From a simple test, it looks like the IQueryable instance created for .Skip(0).Take(1000) is unchanged from .NET 6, but it's quite possible I've overlooked something. Thanks.

@hvkooten
Copy link
Author

hvkooten commented Feb 9, 2023

Hi @cston,
Because of your comment, I've tested the same code in .net 6 and saw in the profiler this sql statement:

exec sp_executesql N'SELECT COUNT(*)
FROM (
    SELECT [p].[Field1], [p].[Field2], [p].[Field3]
    FROM [View] AS [p]
    WHERE [p].[ItemType] = @__Type_0
    ORDER BY (SELECT 1)
    OFFSET @__p_1 ROWS FETCH NEXT @__p_2 ROWS ONLY
) AS [t]',N'@__Type_0 nvarchar(4000),@__p_1 int,@__p_2 int',@__Type_0=N'PO',@__p_1=0,@__p_2=1000

So, there definitive is a change somewhere ;-)

@ajcvickers ajcvickers transferred this issue from dotnet/runtime Feb 9, 2023
@maumar
Copy link
Contributor

maumar commented Feb 10, 2023

looks like a dupe of #29667, which is now fixed in main and approved for 7.0.4 patch

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants