0

I am working with 2 object types array in C#, One of my array is populated from MS Access and other in being filled from MySql.

object[] Product = [{123, "Tea"},{234, "Coffee"},{345, "Drinks"}]; // from MySql

object[] ProductDetails = [{123, "T", 23.00},{234, "C", 25.02},{345, "D", 11.88}]; // from MS Access

I need to loop through its all ProductDetails to match with its ID in Product array , where ID matches it replace the name from product array to productsdetails array,

OR any other approach do it efficiently in c# code. Records may be in thousands.

4
  • 2
    Rather than two arrays, have you considered a Dictionary? Commented Mar 19, 2018 at 10:10
  • 2
    Provide compiling code and the code where you show what you have tried Commented Mar 19, 2018 at 10:11
  • Enumerate throught Ms Access and find match by ID in SQL. Just ensure you have indexes on ID column in SQL. If you want to work in-memory use Dictionary as said by @mjwills Commented Mar 19, 2018 at 10:11
  • you can convert the arrays to your own class or a Dictionary using List<T> , then you can use .Find() to test if your condition is true Commented Mar 19, 2018 at 10:13

2 Answers 2

3

Here you see a good reason why you should not use multi-dimensional object arrays. Instead you should use a Dictionary<TKey, TValue>. I'd suggest to implement a custom class Product. Then the dictionary would be a Dictionary<int, Product>. This would be very efficient and readable.

However, to answer your question and show the mess:

object[,] Product = { { 123, "Tea" }, { 234, "Coffee" }, { 345, "Drinks" } }; // from MySql
object[,] ProductDetails = { { 123, "T", 23.00 }, { 234, "C", 25.02 }, { 345, "D", 11.88 } };

for (int k = 0; k < ProductDetails.GetLength(0); k++)
{
    object id = ProductDetails[k, 0];
    string longName = null;
    bool idFound = false;
    for (int l = 0; l < Product.GetLength(0); l++)
    {
        if (id.Equals(Product[l, 0]))  // Equals necessary because boxing of int to object
        {
            idFound = true;
            longName = Product[l, 1] as string; 
            break;
        }
    }

    if (idFound)
    {
        ProductDetails[k, 1] = longName; 
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

can you please re-write this in dictionary way
@user6594294: Sorry, no, i would have to show you a new class, the way i initialize the dictionary with sample objects and the code to access each elements. You can easily find tutorials for all of this. If you struggle somewhere you can ask another question
0

If you're using linq you could just join the two arrays on the ID (I am unsure if you have to convert them to List() first), and then create a new array where you select want you want from each array.

var result = (from p in Products join pd in ProductDetails where p.Id equals pd.Id select new ...).ToList()

1 Comment

@Agent_Orange because?

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.