我們可以改善這行代碼我們如何改進下面的代碼行?
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;
}
我只是想與以下,但沒有工作...需要幫助
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;
}
我計數8行,而不是一個。你想要「改進」什麼? – Oded 2012-02-29 11:24:15
任何更好的方法來重新編寫陳述 – user1025901 2012-02-29 11:33:20