locked
form and foreach in C# RRS feed

  • Question

  • User87195929 posted

    Hi 

    I have little confussion in forach and form key word in C#

    foreach is defined like below

    int[] arr1 = { 1, 2, 4 };

    foreach(int dd in arr1)
    {

    code
    }

    form keyword in linq as

    var num=from dd in arr where dd>2 select dd

    both from and  foreach used as iteration  ,but in linq dd is not defined with data type

    please explain this 

     thanks and regards

    siddu

    Saturday, April 28, 2018 7:11 PM

All replies

  • User1120430333 posted

    ,but in linq dd is not defined with data type

    dd is a type it is a primitive type int, which is based on the source that is being queried.

    Just like the linq query expression below where country, a type,  is a custom object in the  countries collection with  country having a public property named Area.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/from-clause

    <copied>

    IEnumerable<Country> countryAreaQuery =
    from country in countries
    where country.Area > 500000 //sq km
    select country;

    <end>

    A Linq query returns a result as an individual object, a type, or a collection of objects in a collection.

    If the Linq query returns a result as a collection, then the collection must be iterated over by using a loop.

     

    Saturday, April 28, 2018 8:06 PM
  • User303363814 posted

    You could write your foreach like this

    foreach (var dd in arr1)

    (in fact, if you use the Visual Studio code snippet for foreach this is the code you will get).  You do not need to specify the type of one element of the collection arr1.  The compiler can work it out.

    It is exactly the same in the from clause.  You do not have to specify the type of dd, the compiler knows the type of arr so it also knows the type of each element of arr.

    Just the same as you let the compiler work out the type of your num variable.

    Sunday, April 29, 2018 11:18 PM
  • User-1732172875 posted

    Its instructive to refactor the LINQ to use extension method syntax:

    var num = arr.Where(x => x > 2);  // x has a type (inferred by the compiler) which is simply the T in the IEnumerable<T> of the source sequence/array

    As you can see too, there is nothing forcing a single result element here, the result is all elements that are > 2.

    If we have no call to .Select then its the same as selecting the entire result item, so is equivalent to the keyword LINQ you wrote.

    The extension method syntax is easier to follow, it is just a "pipeline", starting at the left the set of values is passed into "Where" and a subset of values emerges from the "Where".

    Saturday, June 9, 2018 6:03 PM
  • User1949625403 posted

    By default the range variable (dd) and data source (arr) in linq from clause are strongly typed so the type of range variable (dd) automatically determined by the compiler based on the data source variable (arr). 

    For example, if the data source (arr) has a type of IEnumerable<Customer>, then the range variable (dd) is automatically considered as Customer. The only time that you must specify the type explicitly is when the source is a non-generic IEnumerable type such as ArrayList.

    To know more about it check following urls.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/from-clause

    https://www.tutlane.com/tutorial/csharp/csharp-foreach-loop-with-examples

    https://www.tutlane.com/tutorial/linq/linq-syntax-query-syntax-method-syntax

    Thursday, June 21, 2018 1:46 PM