0

I have an array of int values int[] ids. I have a Datatable DataTable dt I want to keep only those values in the array that are there in the Datatable column ids

Say int[] ids contain [2,3,4,5] dt contains [2,3,4,3,4] ---ids here may repeat

so output ids will have only [2,3,4]

Pls suggest ways with lambda or linq....

I tried the crude way using two foreachs.

5 Answers 5

4

use

int[] myIDs = (from d in dt.AsEnumerable() select d.Field<int>("id")).Intersect (ids).ToArray();

For reference see:

Sign up to request clarification or add additional context in comments.

3 Comments

It should be d["id"] instead of dt["id"]. In fact, I'd use d.Field<int>("id") to ensure that the LINQ resultset is a IEnumerable<int> instead of IEnumerable<object>. Oh, and replace dt by dt.AsEnumerable().
Hey Heinzi, can we similarly do for a dictionary as well. dictionary(object, list<DateTime>)!!
@PremanshuMishra you can do something similar for Dictionary too but please ask a new question and describe the specific goal you want to achieve... please don't forget to upvote/mark as accepted any answer that was of help...
1

You need to create a new array. Arrays are fixed size.

If you want a data structure able to remove an element you need a List. Note that List removal operation have a worst case complexity of O(n).

For your particular problem however i would write something like this:

public int[] MyFunc(DataTable dt, int[] array)
{

    Set<int> allowedsIds = new Set<int>();

    Fill your set with ids you want to keep

    int[] newArray = new int[inputArray.Length];
    int newArrayCount = 0;

    for (int i = 0; i < inputArray.Length; ++i)
    {
        if (allowedsIds.Contains(inputArray[i]))
        {
            newArray[newArrayCount++] = inputArray[i];
        }
    }

    Array.Resize(ref newArray, newArrayCount);
    return newArray;
}

1 Comment

Why would List removal be O(n^2)?
0

You need the intersection of the 2 collections. Linq as a Intersect method for that.

From the Linq 101 samples:

public void Linq50()
{
    int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
    int[] numbersB = { 1, 3, 5, 7, 8 };

    var commonNumbers = numbersA.Intersect(numbersB);

    Console.WriteLine("Common numbers shared by both arrays:");
    foreach (var n in commonNumbers)
    {
        Console.WriteLine(n);
    }
}

You can find more examples here in Linq 101 Samples.

Comments

0

Use the Intersect function:

        var ids = new[] {2, 3, 4, 5};
        var dt = new[] {2, 3, 4, 3, 4};

        foreach (var id in ids.Intersect(dt))
        {

        }

Comments

0

You could create List<int> fromDB and (cycling over dataset) fill it with ids column values.
Then you could use:

List<int> result = ids.Intersect(fromDB).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.