我有一个linq查询可以正常工作但是当在项目上运行StyleCop时会产生以下错误:“CA1505:Microsoft.Maintainability:重写或重构类型的方法以增加其可维护性指数(MI)”。我已经测试了删除部分查询,发现它修复了错误。问题似乎是查询太长,所以我正在寻找一种方法来缩短它以消除CA1505错误。这里的挑战是数据驻留在5个主表中,相当于此的sql基本上是5个联合所有语句。有什么建议吗?
public List<ExpiringItemModel> GetByDaysToExpire(int days)
{
var int1 = days - 30;
var int2 = days - 16;
var int3 = days - 0;
var query = (
from outlet in _modelContext.Outlets
join outletcommunicationmethod in _modelContext.OutletCommunicationMethods on
new { Id = outlet.Id }
equals new { Id = outletcommunicationmethod.OutletId }
join outletcarpermitagent in _modelContext.outletcarpermitagents on
new { Id = outlet.Id } equals
new { Id = outletcarpermitagent.OutletId }
join outlettype in _modelContext.OutletTypes on
new { OutletTypeId = outletcarpermitagent.OutletTypeId } equals
new { OutletTypeId = outlettype.Id }
where
(outletcommunicationmethod.CommunicationMethodId == 1
&&
SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletcarpermitagent.BondExpirationDate) > 0) &&
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletcarpermitagent.BondExpirationDate) == days ||
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletcarpermitagent.BondExpirationDate) == 30) ||
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletcarpermitagent.BondExpirationDate) == 14))
select new ExpiringItemModel
{
Id = (Int32)outletcarpermitagent.Id,
AgentType = outlettype.Value,
Email = outletcommunicationmethod.Value,
BondExpirationDate = (DateTime)outletcarpermitagent.BondExpirationDate
}
).Concat
(
from outlet_1 in _modelContext.Outlets
join outletcommunicationmethod_1 in _modelContext.OutletCommunicationMethods on
new { Id = outlet_1.Id } equals new { Id = outletcommunicationmethod_1.OutletId }
join outlettruckpermitagent in _modelContext.outlettruckpermitagents on
new { Id = outlet_1.Id } equals new { Id = outlettruckpermitagent.OutletId }
join outlettype_1 in _modelContext.OutletTypes on
new { OutletTypeId = outlettruckpermitagent.OutletTypeId } equals
new { OutletTypeId = outlettype_1.Id }
where
(outletcommunicationmethod_1.CommunicationMethodId == 1
&&
SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outlettruckpermitagent.BondExpirationDate) > 0) &&
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outlettruckpermitagent.BondExpirationDate) == days ||
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outlettruckpermitagent.BondExpirationDate) == 30) ||
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outlettruckpermitagent.BondExpirationDate) == 14))
select new ExpiringItemModel
{
Id = (Int32)outlettruckpermitagent.Id,
AgentType = outlettype_1.Value,
Email = outletcommunicationmethod_1.Value,
BondExpirationDate = (DateTime)outlettruckpermitagent.BondExpirationDate
}
).Concat
(
from outlet_2 in _modelContext.Outlets
join outletcommunicationmethod_2 in _modelContext.OutletCommunicationMethods on
new { Id = outlet_2.Id } equals new { Id = outletcommunicationmethod_2.OutletId }
join outletcyclepermitagent in _modelContext.outletcyclepermitagents on new { Id = outlet_2.Id } equals
new { Id = outletcyclepermitagent.OutletId }
join outlettype_2 in _modelContext.OutletTypes on new { OutletTypeId = outletcyclepermitagent.OutletTypeId }
equals new { OutletTypeId = outlettype_2.Id }
where
(outletcommunicationmethod_2.CommunicationMethodId == 1
&&
SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletcyclepermitagent.BondExpirationDate) > 0) &&
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletcyclepermitagent.BondExpirationDate) == days ||
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletcyclepermitagent.BondExpirationDate) == 30) ||
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletcyclepermitagent.BondExpirationDate) == 14))
select new ExpiringItemModel
{
Id = (Int32)outletcyclepermitagent.Id,
AgentType = outlettype_2.Value,
Email = outletcommunicationmethod_2.Value,
BondExpirationDate = (DateTime)outletcyclepermitagent.BondExpirationDate
}
).Concat
(
from outlet_3 in _modelContext.Outlets
join outletcommunicationmethod_3 in _modelContext.OutletCommunicationMethods on
new { Id = outlet_3.Id } equals new { Id = outletcommunicationmethod_3.OutletId }
join outletscooteragent in _modelContext.outletscooteragentAgents on
new { Id = outlet_3.Id } equals
new { Id = outletscooteragent.OutletId }
join outlettype_3 in _modelContext.OutletTypes on
new { OutletTypeId = outletscooteragent.OutletTypeId }
equals new { OutletTypeId = outlettype_3.Id }
where
(outletcommunicationmethod_3.CommunicationMethodId == 1
&&
SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletscooteragent.BondExpirationDate) > 0) &&
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletscooteragent.BondExpirationDate) == days ||
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletscooteragent.BondExpirationDate) == 30) ||
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletscooteragent.BondExpirationDate) == 14))
select new ExpiringItemModel
{
Id = (Int32)outletscooteragent.Id,
AgentType = outlettype_3.Value,
Email = outletcommunicationmethod_3.Value,
BondExpirationDate = (DateTime)outletscooteragent.BondExpirationDate
}
).Concat
(
from outlet_4 in _modelContext.Outlets
join outletcommunicationmethod_4 in _modelContext.OutletCommunicationMethods on
new { Id = outlet_4.Id } equals new { Id = outletcommunicationmethod_4.OutletId }
join outletoffroadagent in _modelContext.outletoffroadagentAgents on new { Id = outlet_4.Id } equals
new { Id = outletoffroadagent.OutletId }
join outlettype_4 in _modelContext.OutletTypes on new { OutletTypeId = outletoffroadagent.OutletTypeId }
equals new { OutletTypeId = outlettype_4.Id }
where
(outletcommunicationmethod_4.CommunicationMethodId == 1
&&
SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletoffroadagent.BondExpirationDate) > 0) &&
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletoffroadagent.BondExpirationDate) == days ||
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletoffroadagent.BondExpirationDate) == 30) ||
(SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
outletoffroadagent.BondExpirationDate) == 14))
select new ExpiringItemModel
{
Id = (Int32)outletoffroadagent.Id,
AgentType = outlettype_4.Value,
Email = outletcommunicationmethod_4.Value,
BondExpirationDate = (DateTime)outletoffroadagent.BondExpirationDate
}
);
return query.ToList();
}
答案 0 :(得分:0)
您可以将每个Concat拆分为自己的查询。
例如
var query1 = from outlet in _modelContext.Outlets
...;
var query2 = from outlet in _modelContext.Outlets
...;
var fullquery = query1.Concat(query2).Concat(...);