0

I have files like Avinash_Create.sql, Avinash_Insert.sql, Avinash_Update.sql , Avinash_Delete.sql.

I need to iterate over the files list and group them based on the name and order by create, insert, update, and delete files.

I am finding it difficult difficult to accomplish. This is what I have so far:

   var userGroups = shortfilenames.GroupBy(s => s.Substring(0, s.IndexOf('_')))
         .Select(g => g.OrderBy(x => x.Substring(x.IndexOf('_')).Contains("CREATE"))
                      .ThenBy(x => x.Substring(x.IndexOf('_')).Contains("INSERT"))
                      .ThenBy(x => x.Substring(x.IndexOf('_')).Contains("UPDATE"))
                      .ThenBy(x => x.Substring(x.IndexOf('_')).Contains("DELETE")));  

The above query is grouping by name 'Avinash' but not working for custom ordering. Please help.

Update:

Please see updated query , still it is not sorting properly

5
  • The OrderBy should be outside of your Select Commented Sep 16, 2014 at 19:33
  • Corrected.still the sort logic is not working .. Commented Sep 16, 2014 at 19:40
  • Your .Select(g => g.OrderBy...) should be .Select(g => g).Orderby... (although then you don't need the Select at all). In other words, Select what data you want and then OrderBy. Commented Sep 16, 2014 at 19:41
  • @crashmstr I think he is trying to sort each group, so I think his ordering is in the correct place Commented Sep 16, 2014 at 19:44
  • g is a qroup where as x is an element of group.. Commented Sep 16, 2014 at 19:44

2 Answers 2

2

Uses Split to extract the relevant sections of the string. Converts the result to upper case, which seems to be missing from your attempt. Additionally makes it a bit shorter by using an array to hold your custom sort order and then Array.IndexOf to get a sort order from it, rather than multiple OrderBy/ThenBy.

var ordering = new [] {"CREATE", "INSERT", "UPDATE", "DELETE"};
var results = shortfilenames.GroupBy(s => s.Split('_')[0])
              .Select(g => g.OrderBy(x => Array.IndexOf(ordering, x.Split('_')[1].Split('.')[0].ToUpper())));
Sign up to request clarification or add additional context in comments.

2 Comments

Giving the error : index and location must refer to same location in the string
@Ryan..Simply Amazing :) . Can you please clarify me what is the change that you made now..I am not able to under stand it
1

This seems to be what what you want:

var shortfilenames = new List<string>(){"Avinash_Create.sql" , "Avinash_Insert.sql" , "Avinash_Update.sql" , "Avinash_Delete.sql"};
var userGroups = shortfilenames
    .Select(fn =>
    {
        string fileName = Path.GetFileNameWithoutExtension(fn);
        string[] nameAndAction = fileName.Split('_');
        return new
        {
            extension = Path.GetExtension(fn),
            fileName,
            name = nameAndAction[0],
            action = nameAndAction[1]
        };
    })
    .GroupBy(x => x.name)
    .Select(g => g.OrderByDescending(x => x.action.Equals("CREATE", StringComparison.InvariantCultureIgnoreCase))
                  .ThenByDescending(x => x.action.Equals("INSERT", StringComparison.InvariantCultureIgnoreCase))
                  .ThenByDescending(x => x.action.Equals("UPDATE", StringComparison.InvariantCultureIgnoreCase))
                  .ThenByDescending(x => x.action.Equals("DELETE", StringComparison.InvariantCultureIgnoreCase))
                  .ToList());

foreach (var ug in userGroups)
foreach (var x in ug)
    Console.WriteLine("{0} {1}", x.name, x.action);

prints out:

Avinash Create
Avinash Insert
Avinash Update
Avinash Delete

Presumes that the file-names always contain the underscore.

2 Comments

@Tim..This is sorting great..Thanks for that. but can i just get the file names in the sorted order instead of name and action ?
@SaiAvinash: i don't understand, you can access all properties of the anonymous type. So just use x.fileName instead of x.name or x.action

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.