Introduction to the LINQ
When using a loop (while, do...while, or for) or enumerating (using foreach) a list, you get the value or a range of values. Once you exit the loop, the…
Lesson 1: Introduction to the LINQ
Introduction to the LINQ#
The Language Integrated Query#
Introduction to Querying#
When using a loop (while, do...while, or for) or enumerating (using foreach) a list, you get the value or a range of values. Once you exit the loop, the operation ends and you cannot access the value(s) that was(were) isolated. If you want to get the isolated value or a list of values again, you would have to perform the operation (create the loop), again. In some cases, you may want to prepare and get one value, a few values, or a range of values for later use, or to use over and over again. To do this, you would create a value or a list of values and store that list in a variable, outside of any loop, then use the value or the list of values when needed. As applied to a loop (while, do...while, or for) or foreach, to perform this operation, you use a conditional statement that would examine the list, look for the value(s), get that value or those values that respond(s) to the condition. Any value(s) that respond(s) to the condition is(are) then stored in the new list. This technique of examining a list is referred to as querying. Practical Learning: Introducing LINQ
- Start Microsoft Visual Studio
- On the main menu, click File -> New -> Project...
- In the middle frame, click ASP.NET Web Application (.NET Framework) and change the application Name to AltairRealtors1
- Click OK
- In the New ASP.NET Web Application dialog box, click the MVC icon and click OK
- In the Solution Explorer, right-click Content -> Add -> Style Sheet
- Type Properties
- Click Add
- Change the document as follows: body { } .navbar-fixed-top { background-color: #003366; } .navbar-fixed-bottom { background-color: #003366; } .centralizer { margin: auto; width: 245px; } .copyright { text-align: center; color: cornsilk; }
- In the Solution Explorer, expand View and expand Shared
- Double-click Layout.cshtml to open it
- Change the document as follows: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@ViewBag.Title - Altair Realtors </title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") <link rel="stylesheet" type="text/css" href="~/Content/Properties.css" /> </head> <body> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> @Html.ActionLink("Altair Realtors", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" }) </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li>@Html.ActionLink("Home", "Index", "Home")</li> <li>@Html.ActionLink("Properties Listing", "Listing", "Home")</li> <li>@Html.ActionLink("About", "About", "Home")</li> <li>@Html.ActionLink("Contact", "Contact", "Home")</li> </ul> </div> </div> </div> <div class="container body-content"> @RenderBody() <hr /> <footer class="navbar-fixed-bottom"> <p class="copyright">© @DateTime.Now.Year - Altair Realtors</p> </footer> </div> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false) </body> </html>
- In the Solution Explorer, expand Controllers and double-click HomeController.cs to open it
- Add a method as follows: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace CountryStatistics2.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } public ActionResult Listing() { return View(); } } }
- In the document, right-click Listing and click Add View...
- In the dialog box, make sure the text box displays Listing and click Add
Introduction to the Language INtegrated Query#
To query a list, you can use the Language Integrated Query, abbreviated LINQ.
Creating a Query#
To create a query, you write a statement using words and operators of the LINQ. The most fundamental operation you can perform on LINQ consists of creating, also referred to as selecting, or querying, a list of values, from an existing list. The basic formula to use is:
var sub-list-name = from value-holder in list select value-holder;The var keyword, the assignment operator "=", the from keyword, the in keyword, the select keyword, and the semicolon are required. The sub-list-name is a name of a new variable that will hold the list of values produced by this operation. The value-holder is the name of a variable that will be used to identify each resulting member of this operation. This variable will be equivalent to getting each member of the list and that responds to a condition. The list represents the name of the variable that you would have declared already. The list can be an arrayof numbers. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>States Statistics</title>
</head>
<body>
<h2>States Statistics</h2>
@{
int[] admissions = new int[] { 22, 49, 25, 48, 31, 38, 5, 1, 27, 4 };
var order = from n in admissions select n;
<ul>
@foreach(var number in order)
{
<li>Order of Admission to Union: @number</li>
}
</ul>
}
</body>
</html>To make the code easier to read, you can spread the select statement to various lines. Here is an example:
var order = from n in admissions
select n;This can also be done as follows:
var order = from n
in admissions
select n;In our introduction, we indicated that one of the reasons you create a query is that you can use it in different sections of your code. As a result, you can create a query statement in one section and access it in another section. The items of the list to query can be floating-point numbers, time values, date and time combinations, characters, etc. The rule is that the values must be of the same type or compatible. Practical Learning: Creating a Query
- Change the document as follows: @{ ViewBag.Title = "Altair Realtors - Properties Listing"; } @{ int i = 1; int[] prices = new int[] { 415665, 782575, 675225, 425865, 498675, 318950, 1180500, 215495, 565995, 348500, 685755 }; var values = from n in prices select n; } <h2 class="text-center">Altair Realtors - Properties Listing</h2> <div class="centralizer"> <table border="6"> <tr style="background-color: #003366; color: #FFFFFF"> <td style="width: 30px; text-align: center; color: #FFFFFF;">#</td> <td style="width: 215px; text-align: center">Market Value</td> </tr> @foreach (var number in values) { if (i % 2 == 0) { <tr style="background-color: #B8CCDC"> <td style="text-align: center; color: #0000FF;">@i</td> <td style="text-align: center;">@number.ToString("#,000")</td> </tr> } else { <tr style="background-color: #FFFFFF"> <td style="text-align: center; color: #0000FF;">@i</td> <td style="text-align: center;">@number.ToString("#,000")</td> </tr> } i++; } </table> </div>
- To execute the project, on the main menu, click Debug -> Start Without Debugging:
- Close the browser and return to your programming environment
- The items queried in LINQ can come from a list of strings. To see an example, change the code as follows: @{ ViewBag.Title = "Altair Realtors - Properties Listing"; } @{ int i = 1; string[] cities = new string[] { "Germantown", "Washington", "Bowie", "Columbia", "Bethesda", "Baltimore" }; var names = from name in cities select name; } <h2 class="text-center">Altair Realtors - Properties Listing</h2> <div class="centralizer"> <table border="5"> <tr style="background-color: #003366; color: #fff"> <td style="width: 30px; text-align: center; color: #FFFFFF;">#</td> <td style="width: 100px;">City</td> </tr> @foreach (var city in names) { if (i % 2 == 0) { <tr style="background-color: #b8ccdc"> <td style="text-align: center; color: #0000FF;">@i</td> <td>@city</td> </tr> } else { <tr style="background-color: #fff"> <td style="text-align: center; color: #0000FF;">@i</td> <td>@city</td> </tr> } i++; } </table> </div>
- To execute the project, on the main menu, click Debug -> Start Without Debugging:
- Close the browser and return to your programming environment
- The items can be a list of dates. For an example, change the code as follows: @{ ViewBag.Title = "Altair Realtors - Properties Listing"; } @{ int i = 1; DateTime[] datesPublished = new DateTime[] { new DateTime(2017, 10, 27), new DateTime(2018, 1, 16), new DateTime(2017, 12, 28), new DateTime(2018, 3, 16), new DateTime(2018, 4, 9), new DateTime(2018, 6, 13) }; var dates = from date in datesPublished select date; } <h2 class="text-center">Altair Realtors - Properties Listing</h2> <div class="centralizer"> <table border="5"> <tr style="background-color: #003366; color: #fff"> <td style="width: 30px; text-align: center; color: #FFFFFF;">#</td> <td style="width: 215px;">Publication Date</td> </tr> @foreach (var time in dates) { if (i % 2 == 0) { <tr style="background-color: #b8ccdc"> <td style="text-align: center; color: #0000FF;">@i</td> <td>@time.ToLongDateString()</td> </tr> } else { <tr style="background-color: #fff"> <td style="text-align: center; color: #0000FF;">@i</td> <td>@time.ToLongDateString()</td> </tr> } i++; } </table> </div>
- To execute the project, on the main menu, click Debug -> Start Without Debugging:
- Close the browser and return to your programming environment
- The items can be Boolean values. For an example, change the document as follows: @{ ViewBag.Title = "Altair Realtors - Properties Listing"; } @{ int i = 1; bool[] hasFinishedBasement = new bool[] { false, false, false, true, false, false, true }; var basements = from boolean in hasFinishedBasement select boolean; } <h2 class="text-center">Altair Realtors - Properties Listing</h2> <div class="centralizer"> <table border="5"> <tr style="background-color: #003366; color: #fff"> <td style="width: 30px; text-align: center; color: #FFFFFF;">#</td> <td style="width: 215px;">Property has a Finished Basement</td> </tr> @foreach (var entertaining in basements ) { if (i % 2 == 0) { <tr style="background-color: #b8ccdc"> <td style="text-align: center; color: #0000FF;">@i</td> <td>@ entertaining </td> </tr> } else { <tr style="background-color: #fff"> <td style="text-align: center; color: #0000FF;">@i</td> <td>@ entertaining </td> </tr> } i++; } </table> </div>
- To execute the project, on the main menu, click Debug -> Start Without Debugging:
- Close the browser and return to your programming environment
- In the Solution Explorer, right-click Models -> Add -> Class...
- Type Property as the name of the file
- Press Enter
- Change the class as follows: namespace AltairRealtors1.Models { public enum PropertyType { Unknown, Townhouse, Condominium, Single_Family } public enum Condition { Unknown, Excellent, Good_Shape, Needs_Repair } public class Property { public long PropertyNumber { get; set; } public PropertyType PropertyType { get; set; } public string Address { get; set; } public string City { get; set; } public string Locality { get; set; } public string County { get; set; } public string State { get; set; } public string ZIPCode { get; set; } public Condition Condition { get; set; } public int Bedrooms { get; set; } public float Bathrooms { get; set; } public bool HasFinishedBasement { get; set; } public bool HasIndoorGarage { get; set; } public int Stories { get; set; } public int YearBuilt { get; set; } public long MarketValue { get; set; } } }
Introduction to LINQ and Classes#
Overview#
The values used in a LINQ statement can be objects of a class. For example, instead of using one of the primitive types to create a list as we have done so far, you can create an array of objects. Of course, you can use one of the built-in .NET Framework classes or you can use your own class. The value of the select expression (n in our formula) represents the whole variable, that is, all members of the collection. If you want to get a property (or a member) of the class, apply the period operator to the value of select and access the desired member. Practical Learning: Querying an Array of Objects
- In the Solution Explorer, below Controllers, double-click HomeController.cs to open it
- Change the document as follows: using System.Web.Mvc; namespace AltairRealtors1.Controllers { public class HomeController : Controller { public Models.Property[] Properties; public HomeController() { Properties = new Models.Property[] { new Models.Property() { PropertyNumber = 148506, Address = "4140 Holisto Crt", City = "Germantown", Locality = "Rolling Hills", County = "Montgomery", State = "MD", ZIPCode = "20904", Condition = Models.Condition.Unknown }, new Models.Property() { PropertyNumber = 149227, Address = "1508 Q Str #808", City = "Washington", Locality = "16th Str NW", State = "DC", ZIPCode = "20009", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Good_Shape, Bedrooms = 2, Bathrooms = 2.50F, Stories = 8, YearBuilt = 1996, MarketValue = 415665 }, new Models.Property() { PropertyNumber = 150281, Address = "9205 Arrow Consfield Str", City = "Bowie", Locality = "Glenn Dale", County = "Prince George", State = "MD", ZIPCode = "20715", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Excellent, Bedrooms = 5, Bathrooms = 3.50F, YearBuilt = 2014, MarketValue = 782575 }, new Models.Property() { PropertyNumber = 162703, Address = "58 New York Ave NW #506", City = "Washington", Locality = "Downtown", State = "DC", ZIPCode = "20001", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Excellent, Bedrooms = 1, Bathrooms = 1, HasIndoorGarage = true, Stories = 6, YearBuilt = 2016, MarketValue = 675225 }, new Models.Property() { PropertyNumber = 172804, City = "Columbia", Locality = "Wilde Lake", County = "Howard", PropertyType = Models.PropertyType.Townhouse, Condition = Models.Condition.Good_Shape, Bedrooms = 4, Bathrooms = 2.50F, HasFinishedBasement = true, Stories = 3, YearBuilt = 2008, MarketValue = 425865 }, new Models.Property() { PropertyNumber = 184462, Address = "7715 Sharron Str", City = "College Park", Locality = "Adelphi", County = "Prince George", State = "MD", ZIPCode = "20740", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Excellent, Bedrooms = 4, Bathrooms = 3.50F, HasFinishedBasement = true, Stories = 3, YearBuilt = 1998, MarketValue = 498675 }, new Models.Property() { PropertyNumber = 186263, Address = "8226 Athena Drv NW #404", City = "Washington", Locality = "16th Str", State = "DC", ZIPCode = "20009", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Excellent, Bedrooms = 2, Bathrooms = 2, Stories = 4, YearBuilt = 2000, MarketValue = 318950 }, new Models.Property() { PropertyNumber = 192703, City = "Bethesda", Locality = "Strathmore", ZIPCode = "20814", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Excellent, Bedrooms = 5, Bathrooms = 4.5F, HasFinishedBasement = true, Stories = 3, YearBuilt = 2010, MarketValue = 1180500 }, new Models.Property() { PropertyNumber = 200417, City = "Baltimore", Locality = "Towson", County = "Baltimore", State = "MD", ZIPCode = "21204", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Excellent, Bedrooms = 2, Bathrooms = 1, HasIndoorGarage = true, MarketValue = 215495 }, new Models.Property() { PropertyNumber = 207150, Address = "14250 Parkdoll Rd", City = "Rockville", Locality = "Twinbrook", County = "Montgomery", State = "MD", ZIPCode = "20854", Condition = Models.Condition.Good_Shape, Bedrooms = 3, Bathrooms = 2.50F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 2, YearBuilt = 1988, MarketValue = 565995 }, new Models.Property() { PropertyNumber = 240875, Address = "842 Hempton Street", City = "Charleston", County = "Kanawha", State = "WV", ZIPCode = "25414", PropertyType = Models.PropertyType.Townhouse, Condition = Models.Condition.Unknown, Bedrooms = 3, Bathrooms = 2.50F, MarketValue = 348500 }, new Models.Property() { PropertyNumber = 247472, Address = "1008 Coppen Street", City = "Silver Spring", Locality = "Aspen Hill", County = "Montgomery", State = "MD", ZIPCode = "20906", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Excellent, Bedrooms = 3, Bathrooms = 3, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 3, YearBuilt = 1996, MarketValue = 685755 }, new Models.Property() { PropertyNumber = 269384, City = "Gettysburg", County = "Adams", State = "PA", ZIPCode = "17325" }, new Models.Property() { PropertyNumber = 288570, Address = "10340 Helmes Street #408", City = "Silver Spring", Locality = "Layhill", County = "Montgomery", State = "MD", ZIPCode = "20906", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Good_Shape, Bedrooms = 1, Bathrooms = 1, YearBuilt = 2000, MarketValue = 542750 }, new Models.Property() { PropertyNumber = 294796, Address = "14688 Parrison Street", City = "Falls Church", State = "VA", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Excellent, Bedrooms = 5, Bathrooms = 3.50F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 3, YearBuilt = 1995, MarketValue = 795995}, new Models.Property() { PropertyNumber = 297415, Address = "980 Phorwick Street NW", City = "Washington", Locality = "Columbia Heights", State = "DC", ZIPCode = "20004", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Good_Shape, Bedrooms = 4, Bathrooms = 3.50F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 1, YearBuilt = 2004, MarketValue = 785620 }, new Models.Property() { PropertyNumber = 297446, Address = "14005 Sniders Blvd", City = "Laurel", Locality = "Walmart", County = "Prince George", State = "MD", ZIPCode = "20707", PropertyType = Models.PropertyType.Townhouse, Condition = Models.Condition.Unknown, Bedrooms = 4, Bathrooms = 1.5F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 2, YearBuilt = 2002, MarketValue = 458620 }, new Models.Property() { PropertyNumber = 304750, Address = "5900 24th Street NW #812", City = "Washington", Locality = "Georgetown", State = "DC", ZIPCode = "20008", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Unknown, Bedrooms = 2, Bathrooms = 2, Stories = 6, YearBuilt = 1992, MarketValue = 588224 }, new Models.Property() { PropertyNumber = 304815, Address = "2566 Flavor Street NE", City = "Washington", Locality = "Brookland", State = "DC", PropertyType = Models.PropertyType.Townhouse, Bedrooms = 4, HasFinishedBasement = true, YearBuilt = 1975, MarketValue = 465660 }, new Models.Property() { PropertyNumber = 338475, City = "Silver Spring", Locality = "Colesville", County = "Montgomery", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Excellent, Bedrooms = 4, Bathrooms = 3.50F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 3, YearBuilt = 2012, MarketValue = 505725 }, new Models.Property() { PropertyNumber = 359374, Address = "792 Jamieson Crt", City = "Greenbelt", County = "Prince George", State = "MD", ZIPCode = "20770", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Good_Shape, Bedrooms = 3, Bathrooms = 2.50F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 3, YearBuilt = 1972, MarketValue = 285600 }, new Models.Property() { PropertyNumber = 379586, ZIPCode = "", PropertyType = Models.PropertyType.Townhouse, Condition = Models.Condition.Needs_Repair }, new Models.Property() { PropertyNumber = 384111, Address = "8226 Athena Drv NW #408", City = "Washington", Locality = "16th Str", State = "DC", ZIPCode = "20009", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Good_Shape, Bedrooms = 2, Bathrooms = 2, Stories = 4, YearBuilt = 2000, MarketValue = 335775 }, new Models.Property() { PropertyNumber = 427115, Address = "10232 Truesome Drv", City = "York", County = "York", State = "PA", ZIPCode = "17401", PropertyType = Models.PropertyType.Single_Family, Bedrooms = 2, HasFinishedBasement = true, Stories = 3, YearBuilt = 1982, MarketValue = 186225 }, new Models.Property() { PropertyNumber = 428504, ZIPCode = "", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Needs_Repair }, new Models.Property() { PropertyNumber = 447597, Address = "4201 Vilamar Ave", City = "Hyattsville", Locality = "Riverdale", County = "Prince George", State = "MD", ZIPCode = "20782", PropertyType = Models.PropertyType.Townhouse, Condition = Models.Condition.Excellent, Bedrooms = 3, Bathrooms = 2, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 3, YearBuilt = 1992, MarketValue = 370885}, new Models.Property() { PropertyNumber = 468820, City = "Baltimore", Locality = "Downtown", State = "MD", ZIPCode = "21201", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Good_Shape, Bedrooms = 2, Bathrooms = 2, HasIndoorGarage = true, Stories = 6, YearBuilt = 2006, MarketValue = 485680}, new Models.Property() { PropertyNumber = 475974, Address = "9015 Marvin Crow Ave", City = "Gaithersburg", Locality = "Montgomey Village", County = "Montgomery", State = "MD", ZIPCode = "20872", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Unknown, Bedrooms = 4, Bathrooms = 2.50F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 1, YearBuilt = 1965, MarketValue = 635775}, new Models.Property() { PropertyNumber = 477447, Address = "8226 Athena Drv NW #404", City = "Washington", Locality = "16th Str NW", State = "DC", ZIPCode = "20009", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Excellent, Bedrooms = 2, Bathrooms = 2, Stories = 4, YearBuilt = 2000, MarketValue = 338880}, new Models.Property() { PropertyNumber = 477925, Address = "822 Harrington Drv", City = "Lanham", Locality = "Glenn Dale", County = "Prince George", State = "MD", ZIPCode = "20706", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Excellent, Bedrooms = 4, Bathrooms = 3.50F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 3, YearBuilt = 2011, MarketValue = 475995}, new Models.Property() { PropertyNumber = 482440, Address = "58 New York Ave NW #402", City = "Washington", Locality = "Canada Embassy", State = "DC", ZIPCode = "20001", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Good_Shape, Bedrooms = 2, Bathrooms = 2, HasIndoorGarage = true, Stories = 4, YearBuilt = 2016, MarketValue = 598465}, new Models.Property() { PropertyNumber = 485006, City = "Glenville", County = "Gilmer", State = "WV", ZIPCode = "26351", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Needs_Repair, Bedrooms = 3, Bathrooms = 2.50F, Stories = 1, YearBuilt = 1962, MarketValue = 148600}, new Models.Property() { PropertyNumber = 502840, City = "Bethesda", Locality = "NIH", ZIPCode = "20814", PropertyType = Models.PropertyType.Townhouse, Condition = Models.Condition.Good_Shape, Bedrooms = 3, Bathrooms = 3.50F, HasFinishedBasement = true, Stories = 3, YearBuilt = 1998, MarketValue = 555450}, new Models.Property() { PropertyNumber = 524880, Address = "1640 Lombardo Ave", City = "Silver Spring", Locality = "White Oak", County = "Montgomery", State = "MD", ZIPCode = "20904", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Good_Shape, Bedrooms = 4, Bathrooms = 2.50F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 3, YearBuilt = 1995, MarketValue = 495880}, new Models.Property() { PropertyNumber = 611464, Address = "6366 Lolita Drive", City = "Laurel", Locality = "Mall", County = "Prince George", State = "MD", ZIPCode = "20707", PropertyType = Models.PropertyType.Single_Family, Bedrooms = 4, Bathrooms = 2.50F, HasFinishedBasement = true, Stories = 2, MarketValue = 422625}, new Models.Property() { PropertyNumber = 682630, Address = "6114 Costinha Avenue", City = "Martinsburg", Locality = "Blairton", County = "Berkeley", State = "WV", ZIPCode = "25401", Condition = Models.Condition.Good_Shape, Bedrooms = 4, Bathrooms = 3.50F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 3, YearBuilt = 2005, MarketValue = 375000}, new Models.Property() { PropertyNumber = 688364, Address = "10315 North Hacht Rd", City = "Alexandria", Locality = "Huntington", State = "VA", ZIPCode = "22231", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Excellent, Bedrooms = 4, Bathrooms = 3.50F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 2, YearBuilt = 2000, MarketValue = 620724}, new Models.Property() { PropertyNumber = 693857, Address = "9047 Woodyard Road", City = "York", County = "York", State = "PA", ZIPCode = "17405", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Excellent, Bedrooms = 4, Bathrooms = 3.50F, HasFinishedBasement = true, Stories = 3, YearBuilt = 2002, MarketValue = 348765}, new Models.Property() { PropertyNumber = 704805, City = "Columbia", Locality = "Town Center", County = "Howard", State = "MD", ZIPCode = "", PropertyType = Models.PropertyType.Townhouse, Condition = Models.Condition.Excellent, Bedrooms = 3, Bathrooms = 3.50F, HasFinishedBasement = true, Stories = 3, YearBuilt = 1988, MarketValue = 525750}, new Models.Property() { PropertyNumber = 749562, Address = "495 Parker House Terrace", State = "WV", ZIPCode = "26901", PropertyType = Models.PropertyType.Townhouse, Condition = Models.Condition.Good_Shape, Bedrooms = 3, Bathrooms = 2.50F, HasFinishedBasement = true, Stories = 3, YearBuilt = 2002, MarketValue = 425400}, new Models.Property() { PropertyNumber = 752814, Address = "8226 Athena Drv NW #612", City = "Washington", Locality = "16th Str", State = "DC", ZIPCode = "20009", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Good_Shape, Bedrooms = 2, Bathrooms = 2, Stories = 6, YearBuilt = 2000, MarketValue = 327755}, new Models.Property() { PropertyNumber = 799475, City = "Martinsburg", County = "Berkeley", State = "WV", ZIPCode = "25401", PropertyType = Models.PropertyType.Townhouse, Condition = Models.Condition.Excellent, Bedrooms = 3, Bathrooms = 2.50F, Stories = 3, YearBuilt = 2010, MarketValue = 212580}, new Models.Property() { PropertyNumber = 811155, Address = "10340 Helmes Street #1012", City = "Alexandria", Locality = "Huntington", State = "VA", ZIPCode = "22231", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Good_Shape, Bedrooms = 1, Bathrooms = 1, YearBuilt = 2000, MarketValue = 372450}, new Models.Property() { PropertyNumber = 838465, City = "Columbia", County = "Howard", ZIPCode = "", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Excellent, Bedrooms = 5, Bathrooms = 3.50F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 3, YearBuilt = 2012, MarketValue = 888520}, new Models.Property() { PropertyNumber = 924792, Address = "680 Prushia Rd", City = "Washington", Locality = "Columbia Road", State = "DC", ZIPCode = "20008", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Good_Shape, Bedrooms = 5, Bathrooms = 3.50F, HasFinishedBasement = true, Stories = 3, YearBuilt = 2000, MarketValue = 595595}, new Models.Property() { PropertyNumber = 927409, Address = "9002 Palasko Hwy", City = "McLean", Locality = "Seven-Corner", State = "VA", ZIPCode = "22102", PropertyType = Models.PropertyType.Condominium, Condition = Models.Condition.Excellent, Bedrooms = 1, Bathrooms = 1, HasIndoorGarage = true, Stories = 12, YearBuilt = 2000, MarketValue = 625415}, new Models.Property() { PropertyNumber = 927474, Address = "9522 Lockwood Rd", City = "Arlington", Locality = "Arlington Cemetary", County = "Arlington", State = "VA", ZIPCode = "22203", PropertyType = Models.PropertyType.Townhouse, Bedrooms = 3, Bathrooms = 2.50F, HasFinishedBasement = true, HasIndoorGarage = true, Stories = 12, YearBuilt = 1992, MarketValue = 850500}, new Models.Property() { PropertyNumber = 937495, Address = "608 D St SE", City = "Washington", Locality = "Capitol Hill", State = "DC", PropertyType = Models.PropertyType.Single_Family, Condition = Models.Condition.Excellent, HasIndoorGarage = true, Stories = 3, MarketValue = 1225700 } }; } public ActionResult Index() { return View(); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } public ActionResult Listing() { return View(); } } }
- To query an array of objects, access the Listing.cshtml document and change it as follows: @{ ViewBag.Title = "Altair Realtors - Properties Listing"; } @{ int i = 1; AltairRealtors1.Controllers.HomeController hc = new AltairRealtors1.Controllers.HomeController(); var houses = from prop in hc.Properties select prop; } <h2 class="text-center">Altair Realtors - Properties Listing</h2> <div class="centralizer"> <table border="6"> <tr style="background-color: #003366; color: #FFFFFF"> <td style="width: 30px; text-align: center; color: #FFFFFF;">#</td> <td style="text-align: center">Category</td> <td style="text-align: center">Address</td> <td style="text-align: center">City</td> <td style="text-align: center">Locality</td> <td>County</td> <td style="text-align: center">State</td> <td style="text-align: center">ZIP Code</td> <td>Condition</td> <td>Bed(s)</td> <td>Bath(s)</td> <td>Basement?</td> <td>Garage?</td> <td>Stories</td> <td>Year Built</td> <td style="text-align: right">Market Value</td> </tr> @foreach (var property in houses) { if (i % 2 == 0) { <tr style="background-color: #B8CCDC"> <td style="text-align: center; color: #0000FF;">@i</td> <td style="text-align: center;">@property.PropertyType</td> <td>@property.Address</td> <td>@property.City</td> <td>@property.Locality</td> <td>@property.County</td> <td style="text-align: center">@property.State</td> <td style="text-align: center">@property.ZIPCode</td> <td>@property.Condition</td> <td>@property.Bedrooms</td> <td>@property.Bathrooms</td> <td>@property.HasFinishedBasement</td> <td>@property.HasIndoorGarage</td> <td>@property.Stories</td> <td>@property.YearBuilt</td> <td style="text-align: right">@property.MarketValue.ToString("#,000")</td> </tr> } else { <tr style="background-color: #FFFFFF"> <td style="text-align: center; color: #0000FF;">@i</td> <td style="text-align: center;">@property.PropertyType</td> <td>@property.Address</td> <td>@property.City</td> <td>@property.Locality</td> <td>@property.County</td> <td style="text-align: center">@property.State</td> <td style="text-align: center">@property.ZIPCode</td> <td>@property.Condition</td> <td>@property.Bedrooms</td> <td>@property.Bathrooms</td> <td>@property.HasFinishedBasement</td> <td>@property.HasIndoorGarage</td> <td>@property.Stories</td> <td>@property.YearBuilt</td> <td style="text-align: right">@property.MarketValue.ToString("#,000")</td> </tr> } i++; } </table> </div>
- To execute, on the main menu, click Debug -> Start Without Debugging:
- Close the browser and return to your programming environment
Using a Method#
To perform a more particular operation on a class, you can create a method in it and then call that method in your LINQ statement. This means that, just as you can access a field or a property of a class, you can access any of its internal or public methods. Here is an example of a method created in a class:
public class Employee
{
public int EmployeeNumber;
public string FirstName;
public string LastName;
public decimal HourlySalary;
public Employee(int number = 0,
string firstName = "John",
string lastName = "Doe",
decimal salary = 0M)
{
EmployeeNumber = number;
FirstName = firstName;
LastName = lastName;
HourlySalary = salary;
}
internal string GetFullName()
{
return LastName + ", " + FirstName;
}
}You can then call the method in your LINQ statement. Here is an example:
var fullNames = from names
in empls
select names.GetFullName();
foreach (var member in fullNames)
lbxEmployees.Items.Add(member);Practical Learning: Ending the Lesson
- Close your programming environment
Home Copyright © 2008-2019, C# Key Next

