Pomelo.EntityFrameworkCore.MySql icon indicating copy to clipboard operation
Pomelo.EntityFrameworkCore.MySql copied to clipboard

how to make a contains query of composite key

Open hoxcape opened this issue 2 years ago • 4 comments

The issue

In mysql,I can make a query like:

select * from `Table` where (Id,Name) in ((1,"test"),...);

How can I make a query like above in ef core using pomelo driver?

hoxcape avatar Apr 19 '22 07:04 hoxcape

Row Constructor isn't supported in EF Core, see Expand tuples to multiple columns for more details.

mguinness avatar Apr 19 '22 15:04 mguinness

@mguinness how about this syntax?translate Contains method of object array

var entities= ctx.Entities.Where(e => new[]
{
    new { Id = 1, Type = 1 },
    new { Id = 2, Type = 1 }
}.Contains(new { b.Id, b.Type})).ToList();

hoxcape avatar Apr 20 '22 12:04 hoxcape

That is the syntax that is being proposed, but it is still in planning stage. See similar issue https://github.com/npgsql/efcore.pg/issues/898 for PostgreSQL.

mguinness avatar Apr 20 '22 16:04 mguinness

@hoxcape You should be able to build something semantically equivalent by manually crafting the expression tree of the conditions that you want and use it as the Where() parameter.

If you need some sample code, let me know.

lauxjpn avatar May 13 '22 17:05 lauxjpn