List<string> OngoingClass = new List<String>()
{
"FLEXI","RETAIL","LITE","RTSPL"
};
List<string> ReturnClass = new List<String>()
{
"FLEXI","LITE","RETAIL","RTSPL"
};
Need to Combine those Two List with Certain Conditions. 1 . OngoingClass FLEXI Should Combine with ReturnClass FLEXI - Should not combine with rest of the classes. 2. Same way LITE fare should combine with LITE Fare. 3. Rest of the classes can combine each other.
For Example Result would like
{ "FLEXI" , "FLEXI" }
{ "LITE","LITE"}
{ "RETAIL","RETAIL"}
{ "RETAIL","RTSPL"}
{ "RTSPL","RETAIL"}
{ "RTSPL","RETAIL"}
CodePudding user response:
As I said in comments, There could be better algorithms to get some combinations, but fo this small dataset this will do the trick
List<Tuple<string, string>> resultSequence = new List<Tuple<string, string>>();
OngoingClass.ForEach( item =>
{
ReturnClass.ForEach( item2 =>
{
if ((item == item2 && item2 == "FLEXI") || (item == item2 && item2 == "LITE") || ( item != "FLEXI" && item != "LITE" && item2 != "FLEXI" && item2 != "LITE"))
{
resultSequence.Add( new Tuple<string, string> ( item, item2 ));
}
});
});
Show result
foreach (var item in resultSequence)
{
Console.WriteLine(item);
}
CodePudding user response:
So you want to merge FLEXI and LITE into a single list and all others into a cartesian product. The last exptected result seems to be wrong, it should not be again "RTSPL","RETAIL" but "RTSPL","RTSPL":
string[] mergeClasses = { "FLEXI", "LITE" };
var resultLists = new List<List<string>>();
foreach (string mergeClass in mergeClasses)
{
var mergeList = new List<string>();
if (OngoingClass.Contains(mergeClass))
{
mergeList.Add(mergeClass);
}
if (ReturnClass.Contains(mergeClass))
{
mergeList.Add(mergeClass);
}
if (mergeList.Any())
{
resultLists.Add(mergeList);
}
}
var other = from o in OngoingClass.Except(mergeClasses)
from r in ReturnClass.Except(mergeClasses)
select (o, r);
foreach (var pair in other)
{
resultLists.Add(new List<string> { pair.o, pair.r });
}
CodePudding user response:
This works for me:
var OngoingClass = new List<String>() { "FLEXI", "RETAIL", "LITE", "RTSPL" };
var ReturnClass = new List<String>() { "FLEXI", "LITE", "RETAIL", "RTSPL" };
var SpecialClass = new List<String>() { "FLEXI", "LITE" };
var query =
from o in OngoingClass
from r in ReturnClass
where (SpecialClass.Contains(o) && r == o)
|| (!SpecialClass.Contains(o) && !SpecialClass.Contains(r))
select (o, r);
This allows any values to be "special" and only pair with themselves.
I get this result:
CodePudding user response:
Try following :
List<string> OngoingClass = new List<String>() { "FLEXI","LITE","RETAIL","RTSPL"};
List<string> ReturnClass = new List<String>() { "FLEXI","LITE","RETAIL","RTSPL"}
var results = OngoingClass.Select((x,i) => new object[] { x, ReturnClass[i]}).ToArray();

