简体   繁体   中英

Entity framework for each loop one per one

I have method inside class which needs to remap two tables into dto, but when I loop through tables it gives me whole table instead of one record per each loop, is there a way to tell returned table to give one item per each for loop? and doing it asynchronously? currently code looks something like this:

var simpleDtoList = new List<DtoObject>();

using (var db = new SimpleContext())
{
     await foreach (SimpleA A in db.SimpleATable)
     {
         using (var database = new SimpleContext())
         {
             await foreach (SimpleB B in database.SimpleBTable)
             {
                   var DtoObject = new DtoObject();

                   DtoObject.Id = A.Id;
                   DtoObject.SimpleA = A.Name;
                   DtoObject.SimpleB = B.Name;

                   simpleDtoList.Add(DtoObject);
             }
        }
    }
}
return simpleDtoList;

I've found solution with setting Where equals index of simpleDtoList but I have to remove await, is there a way to keep await code?

Whould something like the following help?

await foreach (SimpleB B in (from b in db.Blogs where something == "somthing").ToListAsync()))
{
     var DtoObject = new DtoObject();
     DtoObject.Id = A.Id;
     DtoObject.SimpleA = A.Name;
     DtoObject.SimpleB = B.Name;
     simpleDtoList.Add(DtoObject);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM