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

Question: .Where with a list of expressions #18568

Closed
buehler opened this issue Oct 24, 2019 · 4 comments
Closed

Question: .Where with a list of expressions #18568

buehler opened this issue Oct 24, 2019 · 4 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@buehler
Copy link

buehler commented Oct 24, 2019

I've been working on a project where I use entity framework core 3.0. Now I have a table with a list of elements that contain two columns which must be matched together. In sql, this query would be super easy, but in expressions, every solution I tried resulted in "Expression cannot be translated".

Since this table has 5 million records in it, it's not an option to load them (as mentioned by EFCore) into the memory and filter them in code.

The SQL I try to achieve would look like:

select *
from "mytyble"
where ("IndicatorId" = 2 AND "ProductId" = 1)
   OR ("IndicatorId" = 3 AND "ProductId" = 1)
   OR ("IndicatorId" = 4 AND "ProductId" = 1)

Is this somehow possible with linq expressions or do I need to prepare the sql statement in code for myself?

Further technical details

EF Core version: Microsoft.EntityFrameworkCore" Version="3.0.0"
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: .net core 3
Operating system: Windows 10
IDE: jetbrains rider

@buehler
Copy link
Author

buehler commented Oct 24, 2019

And yes, I'm sorry that I used "bug" here. Sadly I haven't found a place to put this question :-(

@roji
Copy link
Member

roji commented Oct 24, 2019

Can you please post more information, specifically the LINQ query you tried to use and got an exception on? Without any further information, there's no reason why you wouldn't be able to use regular boolean logic in C# with LINQ and have that translated to SQL.

@smitpatel
Copy link
Contributor

Related #11799

@buehler
Copy link
Author

buehler commented Oct 25, 2019

@roji Of course.

So I have a dynamic list of this combinations, which is why I can't just use boolean c# logic.

public class Selector 
{
    public int Id1 {get;set;}
    public int Id2 {get;set;}
}

public class SubModel
{
    public int Id {get;set;}
    public int Id1 {get;set;}
    public int Id2 {get;set;}
}

public class Model 
{
    public ICollection<SubModel> SubModels {get;set;} = new List<SubModel>();
}

public async Task<IEnumerable<Model>> GetModels(IList<Selector> selectors)
{
    var query = context.Models.AsQueryable();
    query = query.Where(m => m.SubModels.Any(sub => selectors.Any(s => s.Id1 == sub.Id1 && s.Id2 == sub.Id2));
    var result = await query.ToListAsync();
}

I tried it with various approaches: from the models perspective, from the sub models perspective, etc.

But I guess, this is really related as @smitpatel said.
Since the related element is open, and I can solve my problem with FromSqlRaw it's fine for the moment :-)

@buehler buehler closed this as completed Oct 25, 2019
@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. customer-reported and removed type-bug labels Oct 25, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

4 participants