c# - Linq CSV Import with line-numers (index) -


i have import csv file. since line-order important data, need part of import (index). (workaround) following:

        string[] alllines = file.readalllines(sourefilepath, encoding.default);             //add line-index         (int = 0; < alllines.length; i++)         {             alllines[i] = + ";" + alllines[i];         }          _sourcelist = (from line in alllines.skip(1)                        let data = line.split(new[] {';'}, stringsplitoptions.none)                        select                            new mappingsource                                {                                    index = convert.toint32(data[0]),                                    targetentity = data[1].trim(),                                    targetfolder = data[2].trim(),                                    targetlevel = data[3].replace(',', '.').trim(),                                    folderdefinition = data[4].trim(),                                    type = data[5].trim(),                                    sourcefolder = data[6].trim(),                                    sourcelevel = data[7].replace(',', '.').trim(),                                    bucketstructure = data[8].trim()                                }).tolist(); 

i find not elegant. there way index right in linq-statement?

thanx

you can "inject" index method syntax only:

_sourcelist = alllines.skip(1)               .select((line, index) => new{ data = line.split(new[] {';'}, stringsplitoptions.none), line, index})               .select(x => new mappingsource{ index = x.index, targetentity = x.data[0].trim(), ...)               .tolist() 

enumerable.select , where have appropriate overloads.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -