question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chained select clauses, last one disregarded

See original GitHub issue

We have reproduced this issue in EF Core 2.2 and 3.1, on both MSSQL and Oracle (Devart provider) I am not sure whether we are doing something wrong, or have something configured incorrectly or whether it’s an EF Core bug. We have the following IQueryable:

var query = _context.EntityDbSet
    .OrderBy(nameof(Entity.StartDate))
    .Select(e => new Entity 
       { 
            Id = e.Id, 
            RelatedItemId = e.RelatedItemId, 
            StartDate = e.StartDate, 
            EndDate = e.EndDate ... 
        })
    .Skip(0).Take(10)

We then proceed to use this query in several other queries. The Id field is the Key in this table. The issue I have is as follows:

var relatedItems = query.Select(e => e.RelatedItemId).ToList();

Generates the following SQL:

SELECT [e].[RELATED_ITEM_ID]
FROM [ENTITY] AS [e]
ORDER BY [e].[START_DATE]
OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY

This works as expected. But the following doesn’t:

var ids = query.Select(e => e.Id).ToList();

Generates the following SQL:

SELECT [e].[ID], [e].[RELATED_ITEM_ID], [e].[START_DATE], [e].[END_DATE] ... 
FROM [ENTITY] AS [e]
ORDER BY [e].[START_DATE]
OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY

I use ids.Contains in a different query. Obviously this fails, because you must only select a single field to use this query in an IN SQL clause.

I tried putting the original Select cause at the end of the var query = … part of the code. If I completely remove this original select, then it works as expected, selecting only the Id field.

Not sure if this has anything to do with it, but we use a custom value generator for the Id column,

Further technical details

EF Core version: Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET Core 2.2, 3.1) Operating system: IDE: Visual Studio 2019 16.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Jan 28, 2020

@arthur-liberman unfortunately our daily builds feed isn’t in a great state at the moment… You want to use the latest packages with version 5.0.0-alpha.1.* (note the dot after the alpha!). Your nuget.config should look something like the following:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="aspnet-extensions" value="https://dotnetfeed.blob.core.windows.net/aspnet-extensions/index.json" />
    <add key="efcore" value="https://dotnetfeed.blob.core.windows.net/aspnet-entityframeworkcore/index.json" />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>
0reactions
maumarcommented, Jan 28, 2020

@arthur-liberman we discussed it within the team, but unfortunately this doesn’t meet our bar for 3.1.x patch - it only seems to affect the dynamic expression generation, which is quite niche/advanced scenario. So the issue will only be officially fixed in the 5.0 timeframe.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Linq chained SELECT and DISTINCT operators
My query is: If Select and Distinct are chained, will that result in multiple iterations over the List? Thanks. Vikas. c# · linq...
Read more >
5 Snowflake Query Tricks You Aren't Using but Should Be
What is often overlooked is that Snowflake doesn't just make setting up and running queries on a database easier. It also features unique...
Read more >
SELECT — Trino 424 Documentation
The SELECT clause specifies the output of the query. Each select_expression defines a column or columns to be included in the result. ......
Read more >
13.2.20 WITH (Common Table Expressions)
The recursive SELECT part must reference the CTE only once and only in its FROM clause, not in any subquery. It can reference...
Read more >
Documentation: 15: 7.2. Table Expressions
A table expression computes a table. The table expression contains a FROM clause that is optionally followed by WHERE , GROUP BY ,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found