简体   繁体   中英

How can i speed up count of big Linq query?

I have some lists of byte arrays that I combine/check each other with Linq. Those lists and arrays can be different in length. The problem is when i try to count the results. Also if I added the .Take(1) clause things doesn't changes. I'll post some code for better understanding.Function Permute() give me back all the permutations of that specific array.

List<byte[]> firstList =new List<byte[]>();
List<byte[]> secondList=new List<byte[]>();
List<byte[]> thirdList =new List<byte[]>();

 IEnumerable<byte[]> sql = (
                            from rid in firstList
                            from s in secondList
                            from p in thirdList
                            from per in Permute(s)
                            where per.SequenceEqual(p)
                            select rid  
                           );

IEnumerable<byte[]> result = (from s in sql
                              where sql.Count(item =>item.SequenceEqual(s)) == 10
                              select s.ToArray()
                             ).Take(1);
if (result.Count() != 0)
{
  byte[] myByte=result.First();
//Do something
}

When I execute result.Count()!=0 and myByte=result.First() waiting time can took an hour, also if I limit list( first ten elements, second 150 and third 200) . So, is there any way to speed up the count or alternatively to check if result contains something in a "rapid" way?

You can cut your time in half by calling result.FirstOrDefault() , which will return null if there are no results.

This means you only need to iterate once, and it means you never need to iterate over the entire collection (unlike .Count() )


You can also make the query itself faster by writing an efficient IEqualityComparer<byte[]> and calling sql.ToLookup() instead of .Count() in the second query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM