-2

I have a DataTable with around 30 columns it can be very large, one is called id.

In a other List I have 4 columns. I'm thinking of putting it in a DataTable but I'm not sure whats best.

name, id2, month, week

I want to know where id == id2 and what the month is at that id2.

The both id and id2 are strings not numbers.

Anyone have a good idea on how to do this the best/fastest way?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Akjell
  • 99
  • 2
  • 2
  • 12
  • 5
    Join both tables on `id` column using `Linq` and get the `month` – Sriram Sakthivel Mar 31 '15 at 09:03
  • 2
    Post here whatever you tried. – SelvaS Mar 31 '15 at 09:04
  • _"In a other List I have 4 columns"_ What kind of list, what kind of class, can you show it and also what you've already tried? – Tim Schmelter Mar 31 '15 at 09:11
  • ""In a other List I have 4 columns" What kind of list, what kind of class, can you show it and also what you've already tried?" It comes from a text files at the moment I have put it in a DataTable. I was thinking of using a HashTable but didnt work when the id was a string. – Akjell Mar 31 '15 at 09:21

2 Answers2

1

Solution without LINQ:

Loop through the smaller list (either the list with id or the list with id2)

For each list item, search if id exists in the other list.

As you see, it is important to have a fast search (either some index or a O(log n) search algorithm)

DrKoch
  • 9,556
  • 2
  • 34
  • 43
1

You could join both collections with LINQ:

var query = from row in dataTable.AsEnumerable()
            join obj in list
            on row.Field<string>("id") equals obj.id2
            select obj.month;

Enumerable.Join is an efficient way to link since it's using a set under the hood.

Now you can execute this query for example with a foreach loop or with ToList/ToArray to create a collection.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Is this possible also if I have my 2nd list in a DataTable also? – Akjell Mar 31 '15 at 09:34
  • @Akjell: of course you could add all your objects in that list into a `DataTable`. Then you can also join both tables if that is what you want. But you don't need it. A `DataTable` is weakly typed whereas a `List` is strongly typed which is better since you don't need to always cast everything to the correct type. – Tim Schmelter Mar 31 '15 at 09:35