English 中文(简体)
每个X结果都形成一连串的数据吗?
原标题:Every X results form a string of data?
  • 时间:2012-05-26 03:16:16
  •  标签:
  • c#
  • .net-4.0

我有一份清单,有超过1000个独有条目,我想从中形成,50个条目合在一起,在一个字符串中,在网络客户请求中使用。

以下是我所想到的:

string query = string.Empty;
for (int i = 0; i < queryList.Count; i++)
{
    query += queryList[i] + Environment.NewLine;
    if (i % 40 == 1)
    {
        // Send webclient request
        query = string.Empty;
    }
}

由于1也是多重,结果1(不确定如何绕过并发送2个初始值)会触发结果1(不确定如何绕过并发送2个初始值),

另外,当它从我不得不手动发送最后的字符串时,如果总数不是数字的倍数,它就会像它那样发送最后的字符串,它不会在最后触发,一旦它从中流出,我就会得到剩下的剩下的剩下的字符串的残渣。

最佳回答

您可以使用 < a href=> "http://code.google.com/p/morelinq/" rel="nolfollow" > MoreLinq

< a href=" "http://code.google.com/p/morelinq/source/browse/ MoreLinq/Batch.cs" rel="no follow" > MoreLinq.Batch - & gt; 将收藏分成批次。

< a href=" "http://code.google.com/p/morelinq/source/browse/ MoreLinq/Pipe.cs" rel = "no follow" > MoreLinq.Pipe - > 类似选择, 允许使用方法调用

以下查询将把查询列表分为40批,然后一次打印40个项目(类似于有关逻辑)。

对于 Web 客户请求, 请替换 Console. writeLine 替换为 Web 客户请求电话 。

queryList.Batch(40).Pipe(x=>Console.WriteLine(String.Join(Environment.NewLine, x.ToArray()))).ToArray();
问题回答

再加一个复方块...

    if (i % 40 == 1)
    {
        if (i == 1)
            continue;
        // Send webclient request
        query = string.Empty;
    }

您可以使用在 < a href=>" http://msdn.microsoft.com/ en- us/library/ system.linq.enumberable.groupby.aspx" rel = "nofollow" >GroupBy 构建有效载荷。

var data = Enumerable.Range(0, 1000)
    .Select(value => value.ToString());

var payloads = data
    .Select((value, idx) => new {idx, value})

    // Abuse automatic integer division cut off.
    .GroupBy(x => x.idx/40)

    // Transform every group into a concatenated string built
    // from all entries in that group.
    .Select(g => g.Aggregate(String.Empty, (curr, next) => curr + next.value + Environment.NewLine));

foreach (var payload in payloads) {
    // Send payload to webservice.
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签