I'm struggling to find a better way of evaluating two objects that may or may not be a list of strings.
I feel that there must be a better way of doing the below. Does anyone have some advice that can help me please?
I'm getting the objects from a web service, so there is no way of knowing beforehand what type of objects they are.
private bool EvaluateDataLists(object dataLeft, object dataRight)
{
bool isDataLeftList = dataLeft is List<string>;
bool isDataRightList = dataRight is List<string>;
if (isDataLeftList && !isDataRightList)
{
foreach (var entry in dataLeft as List<string>)
{
if (Expression.Evaluate(entry, dataRight.ToString(), valueOperator))
return true;
}
}
else if (!isDataLeftList && isDataRightList)
{
foreach (var entry in dataRight as List<string>)
{
if (Expression.Evaluate(dataLeft.ToString(), entry, valueOperator))
return true;
}
}
else if (isDataLeftList && isDataRightList)
{
foreach (var leftEntry in dataLeft as List<string>)
{
foreach (var rightEntry in dataRight as List<string>)
{
if (Expression.Evaluate(leftEntry, rightEntry, valueOperator))
return true;
}
}
}
return false;
}
If I have understood your question/code correctly does this solve your problem or help?
private bool EvaluateDataLists(object dataLeft, object dataRight)
{
List<string> left = (dataLeft is List<string>) ? dataLeft as List<string> : new List<string> { dataLeft.ToString() };
List<string> right = (dataRight is List<string>) ? dataRight as List<string> : new List<string> { dataRight.ToString() };
foreach (string item in left)
{
if (right.Where(r => Expression.Evaluate(item, r, valueOperator)).Count > 0)
{
return true;
}
}
return false;
}
If so, I would probably refactor the two halves of the method into two separate methods - one to ensure you have lists of strings, and a second do iterate and compare
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.