1

How would I convert the following using LINQ?

foreach (int[] arr in jaggedArray)
{
    if (arr[7] == 1)
    {
        if (!CheckThis(arr))
            boolSuccess = false;
        else
            intCount++;
    }
}
  • what's the purpose of boolSuccess and intCount? – Simone Jun 08 '11 at 10:55
  • Hmm, you can get either one with a LINQ query easily enough, but I can't offhand think of a way to get both in a single query. – Sven Jun 08 '11 at 10:57
  • boolSuccess and intCount are just used further down in the logic – Chris Morris Jun 08 '11 at 11:01
  • @Sven you could use a varaible declared outside teh linq expression to get the second output (closure). My optinion is here that it maybe is more useful to leave this code as is and extract it to a regular method instead of changing to linq code. In this case, the plain old c# code may be much better readable and maintainable. I personally miss some code comments when doing such things as linq... – eFloh Jun 08 '11 at 11:04
  • Do boolSuccess and intCount have values prior to this loop? Or are they effectively initialised by this code? As mentioned [elsewhere](http://stackoverflow.com/questions/200574/linq-equivalent-of-foreach-for-ienumerablet/200614#200614) your linq code probably shouldn't have side effects – Rob Jun 08 '11 at 11:11

6 Answers6

1

Something like this:

var filtered = jaggedArray.Where(arr => arr[7] == 1);
var intCount = filtered.Where(ar => CheckThis(ar)).Count()
var boolSuccess = filtered.Count() == intCount;
Ankur
  • 33,367
  • 2
  • 46
  • 72
  • this code would `Count()` the array twice, which is not very performant as it enumerates `filtered` twice. – eFloh Jun 08 '11 at 11:05
  • Agree, but I believe in making it work first and then if performance sucks (as par the requirement) then fix it :) – Ankur Jun 08 '11 at 11:08
  • "Not very performant": The .Count() calls would become SELECT COUNT() statements, so the IQueryable would not be enumerated at all. – Emyr Jun 08 '11 at 11:09
  • How would you then cast filtered to an int[] array which is then passed to CheckThis? – Chris Morris Jun 08 '11 at 11:10
  • @Chris, check the second line... this is where each array in filtered is passed to CheckThis function... filtered is IEnumerable i.e a set of int arrays and each of them is passed to CheckThis using where clause – Ankur Jun 08 '11 at 11:13
  • @Emyr you are only right if the Enumerable is an IQueryable against an SQL database, what whe don't even know in this case. nevertheless, Ankur is right, perfomance may be added later in this case. – eFloh Jun 08 '11 at 12:46
0

Nested query has to be written for the same logic or else the source code logic has to be simplified first to get a simple query

Zenwalker
  • 1,883
  • 1
  • 14
  • 27
0

I use ReSharper, which suggests when a Linq expression is better - and performs the translation. Sometimes I keep my code though, when the Linq becomes too complex and hard to understand.

Niklas Wulff
  • 3,497
  • 2
  • 22
  • 43
0

I believe this would work: First, it sets intCount by getting all of the items that satisfy arr[7]==1 and pass the CheckThis() method. That should be your intCount value.

Then, if the value in intCount doesn't match the length of the array, at least one thing failed, so you can set boolSuccess to false.

Please note that I consider this solution to be more clever, and less readable. Less readable is always a bad thing. Plus, you could refactor your existing method easily, whereas doing it this way would be much harder to refactor due to the Cleverness Factor.

        intCount = jaggedArray.Where(x => x[7] == 1).Where(CheckThis).Count();
        if (intCount != jaggedArray.Length) boolSuccess = false;
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • ReSharper came up with this, which I am happy with:- `code`foreach (int[] arr in jaggedArray.Where(arr => arr[7] == 1)) { if (!ValidateInterviewDetailsForSend(arr)) boolSuccess = false; else intExportCount++; } – Chris Morris Jun 08 '11 at 11:26
0

You can use Array.ForEach, although I think what you started with is actually clearer

Array.ForEach(jagged, arr => 
{ 
    if (arr[7] == 1) 
    { 
        if (!CheckThis(arr)) 
        { 
            boolSuccess = false; 
        } 
        else 
        { 
            intCount++; 
        } 
     } 
});
Rob
  • 4,327
  • 6
  • 29
  • 55
0
intCount += jaggedArray
  .Where(arr => arr[7] == 1)
  .Select(arr => 
  {
    int val = CheckThis(arr) ? 1 : 0;
    if (val == 0) {boolSuccess = false;}
    return val;
  }).Sum()
Amy B
  • 108,202
  • 21
  • 135
  • 185