C# 如何组合多部分LINQ到实体查询
我在Stack Exchange(测试版)上发布了这段代码片段,以便获得关于如何最好地重构多部分C# 如何组合多部分LINQ到实体查询,c#,asp.net-mvc,linq,entity-framework,linq-to-entities,C#,Asp.net Mvc,Linq,Entity Framework,Linq To Entities,我在Stack Exchange(测试版)上发布了这段代码片段,以便获得关于如何最好地重构多部分LINQ查询的反馈 由于对LINQ比较陌生,我不确定从何处开始这个查询 如果有人能给我一些关于在方法中组合一些LINQ查询的建议,我将不胜感激;尤其是'exclude'IQueryable集合到主查询中(请参见注释) 目前查询的性能不是特别好,如果您能从代码的角度提供任何建议以提高其性能,我们将不胜感激 在代码评审中收到的评论更多的是架构性的,但是目前我还不能将任何内容移动到数据库中 我很感激这是一个
LINQ
查询的反馈
由于对LINQ
比较陌生,我不确定从何处开始这个查询
如果有人能给我一些关于在方法中组合一些LINQ查询的建议,我将不胜感激;尤其是'exclude'IQueryable集合到主查询中(请参见注释)
目前查询的性能不是特别好,如果您能从代码的角度提供任何建议以提高其性能,我们将不胜感激
在代码评审中收到的评论更多的是架构性的,但是目前我还不能将任何内容移动到数据库中
我很感激这是一个很大的方法,但我已经发布了全部内容来提供上下文
提前感谢您提供的任何建议
方法
///按用户名和公司获取模板
公共列表GetItemBrowsingSessionItems(
国际公司,
字符串用户名,
布尔HidePendingDeletes,
Boolean HideWithAppointExpanding,
布尔hideWithCallBacksPending,
int viewMode,
字符串搜索字符串,
列出所需的状态,
列出所需资源,
字符串OrderBy,
浏览会话ReadCustomField(字段筛选器)
{
尝试
{
可液化的除外1;
可液化的除外2;
可液化的除外3;
//准备挂起的回拨
if(hideWithCallBacksPending==true)
{
exclude1=(从分贝引线中的l1开始
其中(l1.Company_ID==companyId)
从l2//隐藏挂起的回调
在db.Tasks中
.其中(o=>(o.IsCompleted??false==false)
&&(o.TaskType_ID==(int)RecordEnums.TaskType.PhoneCall)
&&(o.Type_ID==(int)RecordEnums.RecordType.Lead)
&&(o.Item_ID==l1.Lead_ID)
&&(o.Due_Date>EntityFunctions.AddDays(DateTime.Now,-1))
)
选择l1);
}
其他的
{
exclude1=(从分贝引线中的l1开始
其中(0==1)
选择l1);
}
//准备待决的约会
if(hideWithAppointExpanding==true)
{
exclude2=(从分贝引线中的a1开始
其中(a1.Company_ID==companyId)
从a2//隐藏待定约会
在db.Tasks中
.其中(o=>(o.IsCompleted??false==false)
&&(o.TaskType_ID==(int)RecordEnums.TaskType.Appointment)
&&(o.Type_ID==(int)RecordEnums.RecordType.Lead)
&&(o.Item_ID==a1.Lead_ID)
&&(o.Due_Date>EntityFunctions.AddDays(DateTime.Now,-1))
)
选择a1);
}
其他的
{
exclude2=(从分贝引线中的a1开始
其中(0==1)
选择a1);
}
//准备删除
if(hidependingdelections==true)
{
排除3=(从d1开始,分贝引线
其中(d1.Company_ID==companyId)
从d2//隐藏挂起的删除
在数据库中删除
其中(o=>(o.LeadId==d1.Lead\u ID))
选择d1);
}
其他的
{
排除3=(从d1开始,分贝引线
其中(0==1)
选择d1);
}
//主查询t1.Lead\u ID==o.LeadId&&o.Expiry>=DateTime.Now)
.DefaultIfEmpty()
其中(t1.Company_ID==companyId)
其中((t2.Username==Username)和&(viewMode==1))| |((t1.Owner==Username)和&(viewMode==1))| |((viewMode==2))//由用户所有或模式2(全部查看)
选择t1).Except(exclude1).Except(exclude2).Except(exclude3);
//分别过滤源和状态
如果(RequiredStatus.Count>0)
{
列表=(从列表中的t1开始)
其中(RequiredStatus.Contains(t1.LeadStatus\U ID))
选择t1);
}
如果(requiredSources.Count>0)
{
列表=(从列表中的t1开始)
其中(requiredSources.Contains(t1.LeadSource\U ID))
选择t1);
}
//在这里进行自定义字段筛选吗
if(fieldFilter!=null)
{
字符串stringIntegerValue=Convert.ToString(fieldFilter.IntegerValue);
开关(fieldFilter.FieldTypeId)
{
案例1:
列表=(从列表中的t1开始)
从t2开始
在db.companyLeadCustomFieldValue中
其中(o=>t1.Lead\u ID==o.Lead\u ID&&fieldFilter.TextValue==o.LeadCustomFieldValue\u值)
选择t1);
打破
/// Get templates by username and company
public List<BrowsingSessionItemModel> GetItemBrowsingSessionItems(
int companyId,
string userName,
Boolean hidePendingDeletions,
Boolean hideWithAppointmentsPending,
Boolean hideWithCallBacksPending,
int viewMode,
string searchString,
List<int?> requiredStatuses,
List<int?> requiredSources,
string OrderBy,
BrowsingSessionLeadCustomField fieldFilter)
{
try
{
IQueryable<Lead> exclude1;
IQueryable<Lead> exclude2;
IQueryable<Lead> exclude3;
//To prepare call backs pending
if (hideWithCallBacksPending == true)
{
exclude1 = (from l1 in db.Leads
where (l1.Company_ID == companyId)
from l2 // Hiding Pending Call Backs
in db.Tasks
.Where(o => (o.IsCompleted ?? false == false)
&& (o.TaskType_ID == (int)RecordEnums.TaskType.PhoneCall)
&& (o.Type_ID == (int)RecordEnums.RecordType.Lead)
&& (o.Item_ID == l1.Lead_ID)
&& (o.Due_Date > EntityFunctions.AddDays(DateTime.Now, -1))
)
select l1);
}
else
{
exclude1 = (from l1 in db.Leads
where (0 == 1)
select l1);
}
//To prepare appointments backs pending
if (hideWithAppointmentsPending == true)
{
exclude2 = (from a1 in db.Leads
where (a1.Company_ID == companyId)
from a2 // Hiding Pending Appointments
in db.Tasks
.Where(o => (o.IsCompleted ?? false == false)
&& (o.TaskType_ID == (int)RecordEnums.TaskType.Appointment)
&& (o.Type_ID == (int)RecordEnums.RecordType.Lead)
&& (o.Item_ID == a1.Lead_ID)
&& (o.Due_Date > EntityFunctions.AddDays(DateTime.Now, -1))
)
select a1);
}
else
{
exclude2 = (from a1 in db.Leads
where (0 == 1)
select a1);
}
//To prepare deletions
if (hidePendingDeletions == true)
{
exclude3 = (from d1 in db.Leads
where (d1.Company_ID == companyId)
from d2 // Hiding Pending Deletions
in db.LeadDeletions
.Where(o => (o.LeadId == d1.Lead_ID))
select d1);
}
else
{
exclude3 = (from d1 in db.Leads
where (0 == 1)
select d1);
}
// MAIN QUERY <--
IQueryable<Lead> list = (from t1 in db.Leads
from t2
in db.LeadSubOwners
.Where(o => t1.Lead_ID == o.LeadId && o.Expiry >= DateTime.Now)
.DefaultIfEmpty()
where (t1.Company_ID == companyId)
where ((t2.Username == userName) && (viewMode == 1)) || ((t1.Owner == userName) && (viewMode == 1)) || ((viewMode == 2)) // Either owned by the user or mode 2 (view all)
select t1).Except(exclude1).Except(exclude2).Except(exclude3);
// Filter sources and statuses seperately
if (requiredStatuses.Count > 0)
{
list = (from t1 in list
where (requiredStatuses.Contains(t1.LeadStatus_ID))
select t1);
}
if (requiredSources.Count > 0)
{
list = (from t1 in list
where (requiredSources.Contains(t1.LeadSource_ID))
select t1);
}
// Do custom field filter here
if (fieldFilter != null)
{
string stringIntegerValue = Convert.ToString(fieldFilter.IntegerValue);
switch (fieldFilter.FieldTypeId)
{
case 1:
list = (from t1 in list
from t2
in db.CompanyLeadCustomFieldValues
.Where(o => t1.Lead_ID == o.Lead_ID && fieldFilter.TextValue == o.LeadCustomFieldValue_Value)
select t1);
break;
case 2:
list = (from t1 in list
from t2
in db.CompanyLeadCustomFieldValues
.Where(o => t1.Lead_ID == o.Lead_ID && stringIntegerValue == o.LeadCustomFieldValue_Value)
select t1);
break;
default:
break;
}
}
List<Lead> itemsSorted; // Sort here
if (!String.IsNullOrEmpty(OrderBy))
{
itemsSorted = list.OrderBy(OrderBy).ToList();
}
else
{
itemsSorted = list.ToList();
}
var items = itemsSorted.Select((x, index) => new BrowsingSessionItemModel
{
Id = x.Lead_ID,
Index = index + 1
});
return items.ToList();
}
catch (Exception ex)
{
logger.Info(ex.Message.ToString());
return new List<BrowsingSessionItemModel>();
}
}
false == false
where (0 == 1)
//To prepare call backs pending
var phoneCallTypeId = (int) RecordEnums.TaskType.PhoneCall;
var exclude1 = GetExclude(hideWithCallBacksPending, companyId, phoneCallTypeId);
//To prepare appointments backs pending
var appointmentTypeId = (int) RecordEnums.TaskType.Appointment;
var exclude2 = GetExclude(hideWithCallBacksPending, companyId, appointmentTypeId);
private object GetExclude(bool hideWithCallBacksPending, int companyId, int typeId)
{
return hideWithCallBacksPending
? (from l1 in db.Leads
where (l1.Company_ID == companyId)
from l2
// Hiding Pending Call Backs
in
db.Tasks.Where(
o =>
(o.IsCompleted ?? false) &&
(o.TaskType_ID == typeId) &&
(o.Type_ID == (int) RecordEnums.RecordType.Lead) &&
(o.Item_ID == l1.Lead_ID) &&
(o.Due_Date > EntityFunctions.AddDays(DateTime.Now, -1)))
select l1)
: (from l1 in db.Leads where (0 == 1) select l1);
}
//To prepare deletions
var exclude3 = hidePendingDeletions
? (from d1 in db.Leads
where (d1.Company_ID == companyId)
from d2
// Hiding Pending Deletions
in db.LeadDeletions.Where(o => (o.LeadId == d1.Lead_ID))
select d1)
: (from d1 in db.Leads where (0 == 1) select d1);
// Either owned by the user or mode 2 (view all)
...select t1)
.Except(GetExclude(hideWithCallBacksPending, companyId, phoneCallTypeId))
.Except(GetExclude(hideWithCallBacksPending, companyId, appointmentTypeId))
.Except(exclude3);