Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a List<FileInfo> of files

(我有一个文件的List<FileInfo>)

List<FileInfo> files = GetFiles();

which has about 2 GB of files.

(其中大约有2 GB的文件。)

Now I need to chunk these files into 500MB parts.

(现在,我需要将这些文件分成500MB的部分。)

In this case the result will be 4 List<FileInfo> with the Sum of all Files is below 500MB.

(在这种情况下,结果将是4 List<FileInfo> ,所有文件的总和小于500MB。)

I have no Idea how to apply Sum() on this..

(我不知道如何在此应用Sum() 。)

List<List<FileInfo>> result = files.GroupBy(x => x.Length / 1024 / 1014 < 500)
                                   .Select(x => x.ToList()).ToList();
  ask by Dr. Snail translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
236 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is something that works.

(这是可行的。)

List<FileInfo> files = new List<FileInfo>();
List<List<FileInfo>> listOfLists= new List<List<FileInfo>>();
files.ForEach(x => {
     var match = listOfLists.FirstOrDefault(lf => lf.Sum(f => f.Length) + x.Length < 500*1024*1024);
     if (match != null)
         match.Add(x);
     else
         listOfLists.Add(new List<FileInfo>() { x });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...