i have query want substitue foreach linq because foreach slow how can write codein 1 query code:
ret = new list<reportdata>(); foreach (var item in number) { string = item.substring(0, 11); string b = item.substring(14, 2); string c = item.substring(19, 11); string d = item.substring(33); ret1 = (from in report a.a == && a.b == b && a.c == c && a.d == d && filter.type.contains(a.y) select new reportdata { x = a.x, y = a.y, }); if (ret1 != null && ret1.tolist().count > 0) { ret.addrange(ret1); } }
as mentioned in comments, linq not make foreach faster; if have iterate entire collection, foreach faster linq.
there no need check null or if results exists in inner linq statement; add range since linq query return enumerable.empty<reportdata> if nothing returned query.
if (ret1 != null && ret1.tolist().count > 0) { ret.addrange(ret1); } // becomes ret.addrange(ret1); assumming number collection of string, make sure there no duplicates:
foreach (string item in number.distinct()) if number large list, thousands of items or more, consider using parallel.foreach.
Comments
Post a Comment