本文介绍了如何使用linq使这段代码更有效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用Linq提高此代码的效率
我尝试过:
//获取所有事件
列表< DataRow> Incidents = rm.getData(Token,incdet)。Tables [Client Data]。AsEnumerable()。ToList();
//获取介于日期范围内的事件
var appointmentNoShow = from in events
where(Convert.ToDateTime(a [dateinc]。ToString()) > = firstDayOfMonth&& Convert.ToDateTime(a [dateinc]。ToString())< = lastDayOfMonth)
选择a;
//获得所有伤害
列表< DataRow> injury = rm.getData(Token,incemp)。Tables [Client Data]。AsEnumerable()。ToList();
//将与日期范围内的事件相匹配的伤害列入清单
var result = injury.Where(p => Incidents.Any(p2 => p2 [incid]。 ToString()== p [incid]。ToString()));
//检查伤害是否有特定代码添加到列表
foreach(结果中的var项目)
{
if(item [consqce]。ToString( )==107547|| item [consqce]。ToString()==107548|| item [consqce]。ToString()==107549)
{
IncidentRecord r = new IncidentRecord();
r.ID = item [incid]。ToString();
//r.Date = item [dateinc]。ToString();
QualifyingList.Add(r);
}
}
//选择与QualifyingList中的事件ID匹配的事件以获取日期
var injuryForMonth = Incidents.Where( p => QualifyingList.Any(p2 => p2.ID == p [incid]。ToString()));
解决方案
How would I make this code more efficient using Linq
What I have tried:
//Get all incidents List < DataRow > Incidents = rm.getData(Token, "incdet").Tables["Client Data"].AsEnumerable().ToList(); //get incidents that fall between a date range var appointmentNoShow = from a in Incidents where (Convert.ToDateTime(a["dateinc"].ToString()) >= firstDayOfMonth && Convert.ToDateTime(a["dateinc"].ToString()) <= lastDayOfMonth) select a; //get all injuries List<DataRow> injuries = rm.getData(Token, "incemp").Tables["Client Data"].AsEnumerable().ToList(); //put injuries that match incidents in date range into list var result = injuries.Where(p => Incidents.Any(p2 => p2["incid"].ToString() == p["incid"].ToString())); // check if injuries have specific code add to list foreach (var item in result) { if (item["consqce"].ToString() == "107547" || item["consqce"].ToString() == "107548" || item["consqce"].ToString() == "107549") { IncidentRecord r = new IncidentRecord(); r.ID = item["incid"].ToString(); //r.Date = item["dateinc"].ToString(); QualifyingList.Add(r); } } //select The incident that match the incident ids in the QualifyingList to get the dates var injuriesForMonth = Incidents.Where(p => QualifyingList.Any(p2 => p2.ID == p["incid"].ToString()));
解决方案
这篇关于如何使用linq使这段代码更有效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!