Can we improve this line of code
if ((l_Subscription.PackageInfo.Applications.Where(x => x.DeviceType != DeviceType.UnKnown)).Count() > 0)
{
//l_Subscription.DeviceTypeID = ((l_Subscription.PackageInfo.Applications.Where(x => x.DeviceType == DeviceType.Tablet)).Count() > 0)?()
if ((l_Subscription.PackageInfo.Applications.Where(x => x.DeviceType == DeviceType.Tablet)).Count() > 0)
l_Subscription.DeviceTypeID = (int)DeviceType.Tablet;
else if ((l_Subscription.PackageInfo.Applications.Where(x => x.DeviceType == DeviceType.Phone)).Count() > 0)
l_Subscription.DeviceTypeID = (int)DeviceType.Phone;
}
I just tried with the below but not working…Need help
int cnt = DoOperation(l_Subscription.PackageInfo.Applications, x => x.DeviceType != DeviceType.UnKnown);
if(cnt > 0)
{
int cnt1 = DoOperation(l_Subscription.PackageInfo.Applications, x => x.DeviceType == DeviceType.Tablet);
int cnt2 = DoOperation(l_Subscription.PackageInfo.Applications, x => x.DeviceType == DeviceType.Phone);
if(cnt1 > 0) l_Subscription.DeviceTypeID = (int)DeviceType.Tablet;
else if (cnt2 > 0) l_Subscription.DeviceTypeID = (int)DeviceType.Phone;
}
private int DoOperation<TKey>(List<l_Subscription.PackageInfo.Applications> list, Func<l_Subscription.PackageInfo.Applications, TKey> predicate)
{
int count = 0;
count = list.Where(predicate).Count();
return count;
}
One simple improvement would be to replace
with
Oh, and if that is all your code, you can remove the outer if, because both the inner if condition and the inner else if condition imply the outer if condition, giving you: