How to get ObjectResult from Entity Framework using Identities list

I have HashSet Identity values ​​that I need to use as request values ​​to return an ObjectResult from Entity Framework

Here's the HashSet:

HashSet<int> officeIds = new HashSet<int>();

Here is the query that I am trying to run more or less:

ObjectResult<FilingOffice> offices = ctx.FilingOffice.Where(office => office IN officeIds.ToList());

The "office => office IN officeIds.ToList ()" part of the above is what I can’t get to work, and I didn’t find any samples on the Internet for saving objects, given the list of primary keys.

ctx is System.Data.Objects.ObjectContext

+3
source share
6 answers

Entity Framework , LINQ 2.

OR .

EF Tips , OR.

,

+7

, : ?

:

var entities = db.Entities.WhereIn(x => x.Id, ids);
+2

.

var offices = ctx.FilingOffice.Where(o => officeIds.ToList().Contains(o.Id));

, Entity Framework - , officeIds.ToList() .

+1

LINQ to Entities. Entity SQL, IN.

string entitySql = String.Format("SELECT VALUE O FROM FilingOffice AS O WHERE O.Id IN {{{0}}}", String.Join(",", officeIds.ToList().ConvertAll(officeId => officeId.ToString()).ToArray()));
ObjectQuery offices = new ObjectQuery(entitySql, ctx);

+1

, , , Office , , , , , ,

ObjectResult<FilingOffice> offices = ctx.FilingOffice.Where(office => office.Id IN officeIds.ToList());

, , , .

0

I had a similar problem that I resolved through an internal connection. See My function below.

public IEnumerable<AccountsCache> GetAccountsById(IEnumerable<int> accountIds)
{
    var query =
        from regAccount in registeredAccounts
        join Ids in accountIds on regAccount.AccountId equals Ids
        select regAccount;
    return query;
}

And in your life circumstances

HashSet<int> officeIds = new HashSet<int>();

ObjectResult<FilingOffice> offices = 
    from f in ctx.FilingOffice
    join Ids in officeIds on f.officeId equals Ids
select f;
0
source

Source: https://habr.com/ru/post/1710212/


All Articles