Marius Vasile

Marius Vasile

  • 590
  • 1.9k
  • 149k

asp.net core simplifying linq query use in razor project

Mar 20 2021 6:27 AM
I am using below query and other similar in 90% of my pages. is there a way to have it in one file and call it from there?
 
  1. var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);  
  2. var orgid = await _context.UsersData.Where(s => s.Id == userId).Select(s => s.OrgID).FirstOrDefaultAsync();  
  3.     WOAssetViews = await(from a in _context.WOAssetLocations.Where(s => s.OrgID == orgid)  
  4.                          join b in _context.WOAssetAssets on a.WOALId equals b.WOALId into TempData1  
  5.                          from c in TempData1.DefaultIfEmpty()  
  6.                          join d in _context.WOAssetEquipments on c.WOAId equals d.WOAId into TempData2  
  7.                          from e in TempData2.DefaultIfEmpty()  
  8.                          join f in _context.WOAssetComponents on e.WOAEId equals f.WOAEId into TempData3  
  9.                          from g in TempData3.DefaultIfEmpty()  
  10.                                   select new WOAssetView  
  11.                                   {  
  12.                                       WOALId = a.WOALId,  
  13.                                       AssetLocation = a.AssetLocation,  
  14.                                       AssetID = c.AssetID,  
  15.                                       AssetName = c.AssetName,  
  16.                                       AssetManufacturer = c.AssetManufacturer,  
  17.                                       EquipmentID = e.EquipmentID,  
  18.                                       EquipmentName = e.EquipmentName,  
  19.                                       EquipmentManufacturer = e.EquipmentManufacturer,  
  20.                                       ComponentID = g.ComponentID,  
  21.                                       ComponentName = g.ComponentName,  
  22.                                       ComponentManufacturer = g.ComponentManufacturer  
  23.                                    }).ToListAsync();  
 

Answers (4)