2

I am still learning LINQ so bear with me :). My question, how to convert the following code into LINQ

int count = 0; foreach (var item in settings.FavouritesSetting) { if (item.FavouriteType != Constants.FavouriteType.Folder) count++; } return count.ToString(); 
1
  • 2
    How about installing ReSharper and inspect their suggestions? Commented May 19, 2013 at 9:43

2 Answers 2

11
var count = settings.FavouritesSetting.Count(i => i.FavouriteType != Constants.FavouriteType.Folder); return count.ToString(); 
Sign up to request clarification or add additional context in comments.

2 Comments

Does that take care the increment of count?
@PutraKg Sort of. count will contain the number of matches, i.e. end result. All the incrementings and comparisons are handled internally by Count method.
0
var count = (from item in settings.FavouritesSetting where item.FavouriteType != Constants.FavouriteType.Folder select item).Count(); return count.ToString(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.