如何使用linq使这段代码更有效率 [英] How would I make this code more efficient using linq

查看:69
本文介绍了如何使用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()));

解决方案

Linq不一定能提高效率:它的作用是延迟执行直到需要结果,是一个非常不同的事情。



我建议您使用秒表类来找出代码中的瓶颈所在,并查看可以改进特定代码的方法。没有一个神奇的公式来提高效率,如果有,它肯定不会是Linq - 虽然Linq可以做懒惰的评估和迭代跳过,手动代码通常更快,除非你确切知道你在做什么。

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 does not necessarily make anything more efficient: what it does is delays execution until the results are needed, which is a very different thing.

What I would suggest is that you use the Stopwatch class to find out where the bottlenecks in your code are, and look at ways in which that specific code can be improved. There isn't a "magic formula" to improve efficiency, and if there was, it certainly wouldn't be Linq - although Linq can do lazy evaluation and iteration skipping, manual code is frequently faster unless you know exactly what you are doing.


这篇关于如何使用linq使这段代码更有效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆