The C# / Entity Framework problem: I have object
Account
{
public string AccountId { get; set; }
public string UserId { get; set; }
public string CurrencyId { get; set; }
}
then I need to return all accounts on "user A" which have same currencyId as accounts for "user B"
This is simple SQL query, but I stuck with EF. This is what I tried
public IQueryable<Account> Test(string userA, string userB)
{
var accountsA = GetAccounts().Where(x => x.UserId == userA);
var accountsB = GetAccounts().Where(x => x.UserId == userB);
return accountsA.Join(
accountsB,
acc1 => acc1.CurrencyId,
acc2 => acc2.CurrencyId,
(acc1, acc2) => acc1
);
}
this query works but return a lot of duplicate accounts for userA.
I tried
public IQueryable<Account> Test(string userA, string userB)
{
var accountsA = GetAccounts().Where(x => x.UserId == userA);
var accountsB = GetAccounts().Where(x => x.UserId == userB);
return accountsA.GroupJoin(
accountsB,
acc1 => acc1.CurrencyId,
acc2 => acc2.CurrencyId,
(acc1, acc2) => acc1
);
}
but it crash with
System.InvalidOperationException
HResult=0x80131509
Message=Processing of the LINQ expression 'DbSet<Account>
.Where(x => x.UserId == "userA").GroupJoin(
outer: DbSet<Account>
.Where(x => x.UserId == "userB"),
inner: acc1 => acc1.CurrencyId,
outerKeySelector: acc2 => acc2.CurrencyId,
innerKeySelector: (acc1, acc2) => acc1)' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.
how can I do DISTINCT on EF queries?