重写foreq声明给linq

时间:2013-05-19 09:39:10

标签: c# linq

我还在学习LINQ所以忍受我:)。我的问题是,如何将以下代码转换为LINQ

int count = 0;

foreach (var item in settings.FavouritesSetting)
{
   if (item.FavouriteType != Constants.FavouriteType.Folder)
      count++;
}

return count.ToString();

2 个答案:

答案 0 :(得分:11)

var count = settings.FavouritesSetting.Count(i => i.FavouriteType != Constants.FavouriteType.Folder);

return count.ToString();

答案 1 :(得分:0)

var count = (from item in settings.FavouritesSetting
            where item.FavouriteType != Constants.FavouriteType.Folder
            select item).Count();
return count.ToString();