Huy Do
Huy Do

Reputation: 44

Query a list object by a list int

I have a list of object (with id) and a list int, what is the best way to query that list object provided the list int.

class CommonPartRecord {
  public int Id {get;set;}
  public object Others {get;set;}
}
var listObj = new List<CommonPartRecord>();
// Load listObj
var listId = new List<int>();
// Load listId

Now select the listObj, for those Id is contained in listId, I currently do this way:

var filterItems = listObj.Where(x => listId.Contains(x.Id));

What would be the faster way to perform this? Thanks, Huy

Upvotes: 0

Views: 198

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

var tmpList = new HashSet<int>(listId);
var filterItems = listObj.Where(x => tmpList.Contains(x.Id));

This could give you a performance boost or a performance drop, it depends heavily on the size of both listObj and listId.

You will need to profile it to see if you get any improvement from it.


Explaining the boost or drop:

I am going to use some really exagrated numbers to make the math easier, lets say the following

  • listObj.Where(x => listId.Contains(x.Id)); takes 5 seconds per row.
  • listObj.Where(x => tmpList.Contains(x.Id)) takes 2 seconds per row.
  • var tmpList = new HashSet<int>(listId); takes 10 seconds to build.

Lets plot out the times of how long it would take to process the data varying by the number of rows in listObj

+----------------+------------------------------+----------------------------------+
| Number of rows | Seconds to process with list | Seconds to process with hash set |
+----------------+------------------------------+----------------------------------+
|              1 |                            5 |                               12 |
|              2 |                           10 |                               14 |
|              3 |                           15 |                               16 |
|              4 |                           20 |                               18 |
|              5 |                           25 |                               20 |
|              6 |                           30 |                               22 |
|              7 |                           35 |                               24 |
|              8 |                           40 |                               26 |
+----------------+------------------------------+----------------------------------+

enter image description here

So you can see if listObj has 1 to 3 rows your old way is faster, however once you have 4 rows or more the new way is faster.

(Note I totally made these numbers up, I can guarantee that the per row for HashSet is faster than per row for List, but I can not tell you how much faster. You will need to test to see if the point you get better performance is at 4 rows or 4,000 rows. The only way to know is try both ways and test.)

Upvotes: 1

Related Questions