English 中文(简体)
Funky LINQ 所需
原标题:Funky LINQ Required

下面是我与我合作的CSV护卫。 它在现实中占有更大的地位,但这足以显示一种模式。

www.un.org/Depts/DGACM/index_spanish.htm 请注意,我已经把这一特别志愿人员放在了适当的界线上,只是为了方便地展示我的模式。

www.un.org/Depts/DGACM/index_spanish.htm 在CSV分块后,根据原地块的大小,即地块的长度是变数长度,使指数数量可变。

www.un.org/Depts/DGACM/index_spanish.htm 信函的形式不一定是P,可以是U、O或F

G9999999990001800002777107050,
G9999999990002777107HMNLAQKPRLLHRAQRWJ010,
1,
3,
29,
P,
6.74,
11.23,
07,
U,
5.25,
14.29,
08,
O,
6.89,
16.92,
07,
P,
5,
4,

我想谈谈第5(29)和第6(P)部分,然后错掉了2个要素,然后取下一个要素(07),然后取下一个要素(P)等,直到我结束扼杀。

In this example I will have 29 P 07 P 08 P 07 P

如果能够轻易做到这一点,我就认为,准则将提供一些东西。

增 编

最佳回答
问题回答
line.Split( , )  //split on commas as it seems from your question that s your input
    .Skip(2) //skip the first two entries
    .Where((l, i) => i % 4 == 3 || i % 4 == 0) //take every 3rd and 4th item
    .Skip(1); //skip the first item since the index is divisible by 4

但是,这似乎只字不提法典正在做些什么,至少我要发表评论。

string[] thestrings = source.Split( , );
string lastItem = thestrings.FirstOrDefault();
List<string> keepers = new List<string>();

foreach(string item in thestrings)
{
  if (item == "P")
  {
    if (lastItem != "P")
    {
      keepers.Add(lastItem);
    }
    keepers.Add(item);
  }
  lastItem = item;
}

i done...... 我的法典

//Create a new variable of string
var x = string.Empty;
//Open a new FileStream
using (var fs = new FileStream(path,...))
//Open the StreamReader
using (var sr = new StreamReader(fs))
{
//Read out the whole CSV
   x = sr.ReadToEnd();
}

//Split up the string by  ,  and whitespaces. dismiss empty entries
var stringArray = x.Split(new char[] {  , ,  
 ,  
  }, StringSplitOptions.RemoveEmptyEntries)

//stolen code following 
var myEnumerable = stringArray.Skip(2)
                              .Where((item, index) => index % 4 == 3 || index % 4 == 0)
                              .Skip(1);

说一行,我会使用一个L RegEx,而不是Linq,因为它是汇编的,而且速度要快得多。





相关问题
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. ...

热门标签