C# 感谢您回复我一个真实的问题,即使它是如此简单和直截了当:) /// <summary> /// Supported image extensions. /// The list is sorted cause we have prioritie

C# 感谢您回复我一个真实的问题,即使它是如此简单和直截了当:) /// <summary> /// Supported image extensions. /// The list is sorted cause we have prioritie,c#,linq,validation,C#,Linq,Validation,感谢您回复我一个真实的问题,即使它是如此简单和直截了当:) /// <summary> /// Supported image extensions. /// The list is sorted cause we have priorities here. /// </summary> SortedList<short, string> supportedExtensions = new SortedList<short, string> { {

感谢您回复我一个真实的问题,即使它是如此简单和直截了当:)
/// <summary>
/// Supported image extensions.
/// The list is sorted cause we have priorities here.
/// </summary>
SortedList<short, string> supportedExtensions = new SortedList<short, string> { { 1, "gif" }, { 2, "png" }, { 0, "jpg" }, { 4, "jpeg" } };


var filesList = Directory.GetFiles(string.Concat(Directory.GetCurrentDirectory(), "\\images", "\\"))
                            .Select(s => Tuple.Create(s.Substring(s.LastIndexOf(@"\") + 1, s.LastIndexOf(@".") - 1 - s.LastIndexOf(@"\")), s.Substring(s.LastIndexOf(@".") + 1)))
                            .Where(x => supportedExtensions.Values.Contains(x.Item2)) // only supported extensions here!
                            .GroupBy(g => g.Item1) // we are assured that every group has proper and at least one value
                            .Select(group => group.OrderBy(o => supportedExtensions.IndexOfValue(o.Item2)).First()); // get only one extension in each name group by priorities as in sorted list
var fileListGroupedBySupportedExtension = Directory.GetFiles(Directory.GetFiles(string.Concat(Directory.GetCurrentDirectory(), "\\images", "\\")))
                            .Select(s => Tuple.Create(s.Substring(s.LastIndexOf(@"\") + 1, s.LastIndexOf(@".") - 1 - s.LastIndexOf(@"\")), s.Substring(s.LastIndexOf(@".") + 1)))
                            .GroupBy(g => supportedExtensions.Values.Contains(g.Item2));

var fileListWithSupportedExtension = fileListGroupedBySupportedExtension
                                .Where(gr => gr.Key == true);
  //DONE: You're trying to use Sorted List as a Dictionary
  Dictionary<string, int> supportedExtensions = 
    new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase) {
      {".gif", 1},
      {".png", 2},
      {".jpg", 0},
      {".jpeg", 4},
    };

  //DONE: Enumerate - do not retrieve all the files and then continue working
  //DONE: Do no reinvent the wheel - Path class 
  var filesList = Directory
    .EnumerateFiles(Path.Combine(Directory.GetCurrentDirectory(), "images"))
    .Where(file => supportedExtensions.ContainsKey(Path.GetExtension(file)))
    .GroupBy(file => Path.GetFileNameWithoutExtension(file))
    .Select(group => group
       .OrderBy(o => supportedExtensions[o])
       .First());
  //.ToArray(); // <- if you want the final materialization
  var filesList = Directory
    .EnumerateFiles(Path.Combine(Directory.GetCurrentDirectory(), "images"))
    .Select(file => {
       string extension = Path.GetExtension(file);      
       int extIndex;
       bool wanted = supportedExtensions.TryGetValue(extension, out extIndex);       

       return new {
         name = file,
         title = Path.GetFileNameWithoutExtension(file),
         extension = extension,
         extIndex = wanted ? extIndex : -1,
         wanted = wanted, }; })
    .Where(item => item.wanted) // you may want to comment out this condition
    .GroupBy(item => item.title)  
    .Select(group => group
       .OrderBy(item => item.extIndex)
       .First()
       .name);
List<string> supportedExtensions = new List<string> {".jpg", ".gif", ".png", ".jpeg"};// Supported extensions in valid order

var filesList = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), "\\images")).Select(x => x.ToLower()).ToList(); // Get all files from directory
var filesWithoutSupportedExtensions = filesList.Where(x => !supportedExtensions.Contains(Path.GetExtension(x))).ToList(); // Get files that do not have supported extension
var filesWithSupportedExtensions = filesList.Where(x => supportedExtensions.Contains(Path.GetExtension(x))).ToList(); // Get files with supported extension
var firstFilesWithUniqueName = filesWithSupportedExtensions.GroupBy(x => Path.Combine(Path.GetDirectoryName(x), Path.GetFileNameWithoutExtension(x)))
    .Select(g => g.OrderBy(f => supportedExtensions.IndexOf(Path.GetExtension(f))).First()).ToList(); // Get file with unique name that has first extension from list
var otherFiles = filesWithSupportedExtensions.Except(firstFilesWithUniqueName).ToList(); // Get all other files

Console.WriteLine(string.Join(Environment.NewLine, firstFilesWithUniqueName));
Console.WriteLine("Files without supported extensions:");
Console.WriteLine(string.Join(Environment.NewLine, filesWithoutSupportedExtensions));

Console.WriteLine("Files with duplicated name and different extension:");
Console.WriteLine(string.Join(Environment.NewLine, otherFiles));
Console.ReadKey();
List<string> all = AllFiles();

IEnumerable<string> matching    = all.Where(f => Condition(f));
IEnumerable<string> notMatching = all.Except(matching);