我是 linq 查询新手,下面显示的 linq 查询需要很长时间才能执行,
List<InventoyModel> vehic = new List<InventoyModel>();
vehic = Winv.Select(x => new InventoyModel
{
InvID = x.Id,
WeaponBrand = WH.LKP_WeaponBrand.Where(b => b.BrandID == x.WeaponBrand).Select(b => b.BrandName).FirstOrDefault(),
LKP_WeaponBrand = x.WeaponBrand,
weaponModel = WH.LKP_WeaponModel.Where(m => m.ModelID == x.weaponModel).Select(b => b.ModelName).FirstOrDefault(),
LKP_WeaponModel = x.weaponModel,
WeaponType = WH.LKP_WeaponType.Where(b => b.TypeID == x.WeaponType).Select(b => b.WeaponName).FirstOrDefault(),
EntityCategoryID = x.EntityCategoryID,
// LKP_WeaponType=x.WeaponType,
SerialNo = x.Serial,
//LKP_WeaponSerial=x.Serial,
MadeOf = WH.LKP_BrandOrigin.Where(b => b.ID == x.MadeOf).Select(b => b.CountryName).FirstOrDefault(),
// LKP_BrandOrigin=Convert.ToInt32(x.MadeOf),
BulletCount = x.BuletCount,
AssignerCategoryID = x.AssignerCategoryID,
WeaponNote = x.Note,
EntityType = x.EntityType,
AssignedBy = x.AssignedBy,
EntityStatusName = WH.LKP_EntityStatus.Where(l => l.StatusID == x.EntityStatus).Select(l => l.Status).FirstOrDefault(),
IsFile = x.IsFile,
MarkazName = x.MarkazName,
MahafzaName = x.MahafzaName,
NID = x.EmpId.ToString().Trim(),
Datetime = x.Date,
UpdateDate = x.ModifyDate,
ModifierName = x.ModifyBy,
EntityQty = x.WeaponQty,
SCEntityQty = 0
}).ToList();
我知道实际上我在查询中使用了许多查询来获取产生问题的值,但我不知道如何解决它。
希望您的建议 谢谢
您的原始查询产生 5 个 OUTER APPLY 查询,这是无效的。因为它看起来像是在主键上连接,所以我们可以用 LEFT JOIN 替换它们。
var query =
from x in Winv
from wb in WH.LKP_WeaponBrand.Where(wb => wb.BrandID == x.WeaponBrand).DefaultIfEmpty()
from wm in WH.LKP_WeaponModel.Where(wm => wm.ModelID == x.weaponModel).DefaultIfEmpty()
from wt in WH.LKP_WeaponType.Where(wt => wt.TypeID == x.WeaponType).DefaultIfEmpty()
from bo in WH.LKP_BrandOrigin.Where(bo => bo.ID == x.MadeOf).DefaultIfEmpty()
from es in WH.LKP_EntityStatus.Where(es => es.StatusID == x.EntityStatus).DefaultIfEmpty()
select new InventoyModel
{
InvID = x.Id,
WeaponBrand = wb.BrandName,
LKP_WeaponBrand = x.WeaponBrand,
weaponModel = wm.ModelName,
LKP_WeaponModel = x.weaponModel,
WeaponType = wt.WeaponName,
EntityCategoryID = x.EntityCategoryID,
// LKP_WeaponType=x.WeaponType,
SerialNo = x.Serial,
//LKP_WeaponSerial=x.Serial,
MadeOf = bo.CountryName,
// LKP_BrandOrigin=Convert.ToInt32(x.MadeOf),
BulletCount = x.BuletCount,
AssignerCategoryID = x.AssignerCategoryID,
WeaponNote = x.Note,
EntityType = x.EntityType,
AssignedBy = x.AssignedBy,
EntityStatusName = es.Status,
IsFile = x.IsFile,
MarkazName = x.MarkazName,
MahafzaName = x.MahafzaName,
NID = x.EmpId.ToString().Trim(),
Datetime = x.Date,
UpdateDate = x.ModifyDate,
ModifierName = x.ModifyBy,
EntityQty = x.WeaponQty,
SCEntityQty = 0
};
var vehic = query.ToList();
考虑在模型类中引入导航属性。这将简化查询并消除连接的需要。通过适当的导航属性,可以简化您的查询:
var query = Winv
.Select(x => new InventoyModel
{
InvID = x.Id,
WeaponBrand = x.Brand.BrandName,
LKP_WeaponBrand = x.WeaponBrand,
weaponModel = x.Model.ModelName,
LKP_WeaponModel = x.weaponModel,
WeaponType = x.Weapon.WeaponName,
EntityCategoryID = x.EntityCategoryID,
// LKP_WeaponType=x.WeaponType,
SerialNo = x.Serial,
//LKP_WeaponSerial=x.Serial,
MadeOf = x.MadeOfOrigin.CountryName,
// LKP_BrandOrigin=Convert.ToInt32(x.MadeOf),
BulletCount = x.BuletCount,
AssignerCategoryID = x.AssignerCategoryID,
WeaponNote = x.Note,
EntityType = x.EntityType,
AssignedBy = x.AssignedBy,
EntityStatusName = x.EntityStatus.Status,
IsFile = x.IsFile,
MarkazName = x.MarkazName,
MahafzaName = x.MahafzaName,
NID = x.EmpId.ToString().Trim(),
Datetime = x.Date,
UpdateDate = x.ModifyDate,
ModifierName = x.ModifyBy,
EntityQty = x.WeaponQty,
SCEntityQty = 0
});
var vehic = query.ToList();