user5333955
user5333955

Reputation: 31

Explain line of code specifically where it says t

I would like to know what the tin the line of code below stands for. Is the "t" user defined or is it dependent on the database?

Survey_Time_Period[] getTimePeriods = 
     (from t in dc.Survey_Time_Periods 
        where t.display == true orderby t.sort descending select t)
     .ToArray();

Upvotes: 0

Views: 74

Answers (2)

mason
mason

Reputation: 32694

This syntax is a LINQ query. You can see a lot of great LINQ examples here.

t is a range variable representing an enumerated instance of whatever type is enumerated from dc.Survey_Time_Periods, in this case a single Survey_Time_Period object. t is only valid within the context of the LINQ query. Later on in the LINQ query, it's filtering on the Survey_Time_Period.display property and then ordering by the Survey_Time_Period.sort property, then returning the resulting instances of Survey_Time_Period.

There are two LINQ syntaxes, and it may be helpful for you to see the equivalent statement in the other LINQ syntax. This alternate syntax utilizes lambda expressions.

Survey_Time_Period[] getTimePeriods = dc.Survey_Time_Periods
    .Where(t => t.display == true)
    .OrderByDescending(t => t.sort)
    .ToArray();

Note that your variable names aren't in keeping with the C# standards. Don't use underscores in variable names, and don't prefix a variable with a verb. Also, comparing a boolean to true isn't necessary, because the boolean already represents true or false. If you adjust your type names and variable names, it becomes a little cleaner:

var timePeriods = dc.SurveyTimePeriods
    .Where(stp => stp.Display)
    .OrderByDescending(stp => stp.Sort)
    .ToArray();

Upvotes: 3

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186698

Let's rewrite Linq in a different format:

Survey_Time_Period[] getTimePeriods = dc.Survey_Time_Periods // Source
  .Where(item => item.display)           // Filter
  .OrderByDescending(item => item.sort)  // Sorting
  .ToArray();                            // Materialization (representation)

that means:

  1. Select all items from dc.Survey_Time_Periods
  2. Such that item.display == true (filter)
  3. Order the filtered items by item.sort in descending order
  4. Represent the filtered and ordered items as array

Upvotes: 1

Related Questions