-1

I'm currently working with culture Info and need to translate some words. For the days, i use this :

--> input : an array DayDetails : [MO,TU, WE, TH, FR] or [MO, WE,TH,FR,SA] ...

 if ( daysDetails.Length == 7 )
    {
       return langSpec.GetLocalMessage( "every" ) + " " + langSpec.GetLocalMessage( "Days" );
    }
    else if( daysDetails.Length == 5 && daysDetails.Contains( "MO" ) && daysDetails.Contains( "TU" ) && daysDetails.Contains( "WE" ) && daysDetails.Contains( "TH" ) && daysDetails.Contains( "FR" ) )
    {
       return langSpec.GetLocalMessage( "workingDays" );
    }

output : a string with : Monday, Thuesday, Wednesday

if there's 5 days and it's the working days i want : a stringwith : working days

if there's 7 days i want : a stringwith "every days"

the thing is, i don't like how my second if looks but i have to test, in case of 5 days, that the days are the working days to proper translate it.

Is there a better way to write this, other than multiple && ?

Sorry for my bad english speaking, hope you'll understand.

EDIT : DayDetails can be also [MO,TH], or [MO] or [FR,WE,TH,TU,MO] since it's a response i got from somewhere else and i can't master the input.

EDIT 2 : i checked Compare two List<T> objects for equality, ignoring order since multiple people suggested it, but it does not really answer to my question. the answer given is way more complicated that my initial "if" with 5 statements.

Zohnya
  • 25
  • 1
  • 7
  • 1
    Can you show us what is input and what is expected output? – Prasad Telkikar Jan 16 '20 at 13:06
  • Why do you *hardcode* don't use `CultureInfo`? E.g. `Console.Write(string.Join(" ", CultureInfo.GetCultureInfo("ru-RU").DateTimeFormat.AbbreviatedDayNames));` prints out Russian day names (`Вс Пн Вт Ср Чт Пт Сб`) – Dmitry Bychenko Jan 16 '20 at 13:07
  • Answer is already provided here: https://stackoverflow.com/questions/38573437/c-sharp-multiple-string-contains – donatasj87 Jan 16 '20 at 13:12
  • Does this answer your question? [Compare two List objects for equality, ignoring order](https://stackoverflow.com/questions/3669970/compare-two-listt-objects-for-equality-ignoring-order) – Thomas Weller Jan 16 '20 at 13:17
  • Is there any possibility to get, say `{"Mon", "Tue", "Wed", "Thu", "Fri"}`? – Dmitry Bychenko Jan 16 '20 at 13:22
  • @donatasj87 your link is for "OR" but i want "AND" – Zohnya Jan 16 '20 at 13:27
  • @DmitryBychenko , not in my input (i don't create the dayDetails, it's just what i receive) – Zohnya Jan 16 '20 at 13:42

2 Answers2

2

Make an array and use All:

    private static readonly List<string> workdays = new List<string>() { "MO", "TU", "WE", "TH", "FR" };
    private static bool isWorkday(string[] daysDetails) => workdays.All(d => daysDetails.Contains(d));
Peter Krassoi
  • 571
  • 3
  • 11
  • that's might be what i need. but i can use the "bool" part. It says me that he cannot convert lambda expression to bool – Zohnya Jan 16 '20 at 13:53
  • nevermind, it was my bad. i ended with something like that : `else if ( workdays.All( d => daysDetails.Contains( d ) ) )` – Zohnya Jan 16 '20 at 13:57
0

You can use Enumerable.SequenceEqual as follows:

string[] dayDetails = new [] { "MO", "TU", "WE", "TH", "FR" };

if (daysDetails.Length == 7)
{
   return langSpec.GetLocalMessage( "every" ) + " " + langSpec.GetLocalMessage( "Days" );
}
else if (Enumerable.SequenceEqual(dayDetails, new [] { "MO", "TU", "WE", "TH", "FR" }))
{
   return langSpec.GetLocalMessage( "workingDays" );
}

Assuming dayDetails will be in the correct order.

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35