Home > front end >  Can I groupby with conditions and return the source type value
Can I groupby with conditions and return the source type value

Time:05-30

I have following scripts data:

Database File
master sql_master
myDB sql_sameGroup1
myDB sql_sameGroup2
myDB sql_sameGroup3
myDB sql_special

scripts.GroupBy(script => new { script.Database, script.File}) will make it 5 groups but I only want 3 groups - The rule is that distinguish by Database, then for same Database, if File != sql_special, I want to put it into the same group - this means in the above table Row1 is group1, Row 2,3,4 is group2, Row5 is group3. and return the scripts type for further processing.

I didn't find suitable GroupBy method overload. how can I achieve that?

CodePudding user response:

Keep grouping by both the Database and File property, but instead of the whole File property, just check if it is sql_special or not.

scripts
    .GroupBy(script => new 
    { 
        script.Database, 
        IsSqlSpecial = script.File is "sql_special" 
    })

Runnable online version

  • Related