1

I have a database that I call select all of its contents of a table. It has 18000+ items. I have a method uses a web service that can have an array of up to ten element pass into it. Right now I am doing item by item instead of by an array. I want to create an array of ten and then call the function. I could make an array of ten and then call the function be what is I have an extra three records?

public static void Main()
{
    inventoryBLL inv = new inventoryBLL();
    DataSet1.sDataTable dtsku = inv.SelectEverything();
    foreach (DataSet1.Row row in dtsku)
    {
        webservicefunction(row.item);
    }
}

My question is how would I transform this?

2 Answers 2

1

Generic solution of your problem could look like this:

static class LinqHelper
{
    public static IEnumerable<T[]> SplitIntoGroups<T>(this IEnumerable<T> items, int N)
    {
        if (items == null || N < 1)
            yield break;

        T[] group = new T[N];
        int size = 0;
        var iter = items.GetEnumerator();

        while (iter.MoveNext())
        {
            group[size++] = iter.Current;
            if (size == N)
            {
                yield return group;
                size = 0;
                group = new T[N];
            }
        }
        if (size > 0)
            yield return group.Take(size).ToArray();
    }
}

So your Main function become

public static void Main()
{
    inventoryBLL inv = new inventoryBLL();
    DataSet1.sDataTable dtsku = inv.SelectEverything();
    foreach (var items in dtsku.Select(r => r.item).SplitIntoGroups(10))
    {
        webservicefunction(items);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I get two errors 1 Cannot convert lambda expersion to type 'string' because it is not a delegate type, 2 Does not contain for 'item'.
Please most more details about your code. As minimum provide DataSet1.sDataTable and DataSet1.Row class declarations and exact signature of your webservicefunction. Otherwise it is difficult to say why you have such errors.
0
var taken = 0;
var takecount = 10;
while(list.Count() >= taken)
{
     callWebService(list.Skip(taken).Take(takecount));
     taken += takecount;
}

Generic Extension Method version:

public static void AtATime<T>(this IEnumerable<T> list, int eachTime, Action<IEnumerable<T>> action)
{
      var taken = 0;
      while(list.Count() >= taken)
      {
           action(list.Skip(taken).Take(eachTime));
           taken += eachTime;    
      }
}

Usage:

inv.SelectEverything().AtATime<Row>(10, webservicefunction);

5 Comments

this is really confusing because it is seemingly in a vacuum.
I don't understand your comment. The code(pseudocode?) provided will take a list and while there are items to process it will send them 10 at a time to callWebService. Isn't that what you want?
This works out, but not optimal: skip needs to start from the beginning in each call until it reaches taken.
And Count() needs to iterate the complete collection
Yeah, good points, but it's pretty and OP doesn't specify any performance constraints.

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.