Baahubali
Baahubali

Reputation: 4830

int array cannot use length property inside linq expression

I have this very simple c# statement

       var list = new int[4];

        var query1 = from element in list
                     where element.Length > 10
                     select element;

and for some reason it's not letting me use Length property of int array and throwing me a following exception:

'int' does not contain a definition for 'Length' and no extension method 
'Length' accepting a first argument of type 'int' could be found (press F4 
to add a using directive or assembly reference)

i cannot figure what's going on. if i write the following:

 var query1 = from element in list.Length
                     where element > 10
                     select element;

then it works.

Upvotes: 0

Views: 1440

Answers (6)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186843

The compiler is right: int doesn't have any Length property (and list is an array of ints). Do you mean string representation of int?

var list = new int[4];
...

var query = list
  .Where(element => element.ToString().Length > 10) // note "ToString()"
  .Select(element);

Please note, that the query, probably, will return an empty enumerable: the longest string representation of int in most cultures is -2147483648 which has exactly 10 digits. If you want count digits (e.g. -123 has three digits) you can modify the query a little:

var query = list
  .Where(element => element
    .ToString(CultureInfo.InvariantCulture)
    .Trim('-')
    .Length > 10) 
  .Select(element);

And in this case, you will definitely have an empty result.

Upvotes: 1

Vadim Vnukov
Vadim Vnukov

Reputation: 174

Your code is similar to this:

var list = new int[4];            
var filteredElements = new List<int>();
foreach (int element in list)
{
  if(element.Length > 10)
  {
    filteredElements.Add(element);
  }
}

That's why you get error 'int' does not contain a definition for 'Length' and no extension method

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 157136

element is an int retrieved from the array list. It isn't the array itself.

Since int doesn't have a Length property, it doesn't work. If you are looking for the number of digits of the integer, take a look here: How can I get a count of the total number of digits in a number?.

The end result would be:

var list = new int[4];

var query1 = from element in list
             where (element == 0 ? 1 : Math.Floor(Math.Log10(Math.Abs(element)) + 1)) > 10
             select element;

Upvotes: 1

usamazf
usamazf

Reputation: 3215

The issue is with your query:

   var list = new int[4];

    var query1 = from element in list
                 where element.Length > 10
                 select element;

Here the element is an individual integer from the array listand not a int[]. So obviously Length doesn't exist for int type.

Upvotes: 1

Ivan Gritsenko
Ivan Gritsenko

Reputation: 4226

  1. When you write from element in list you say that you want to enumerate over each element of list so element is of type int. No Length property on int type exists. Hence the error.

  2. When you write from element in list.Length you want to enumerate over single item which is the length of the array and that item is of type int.

Upvotes: 1

Marnix
Marnix

Reputation: 6547

from element means that linq is going to loop over all ints in list and every int is from then on called element.

See it as a foreach loop. You now wrote something like this:

foreach(int element in list)
{
    if(element.Length > 10) // an int doesn't have a Length property
    {
        yield return element;
    }
}

I am not sure what you are trying to achieve with the length property, but this is what you wrote. Could you describe what you want to assess with the length property?

Upvotes: 2

Related Questions