Home

Introduction to Classes

Fundamentals of Classes

Introduction

A class is a technique to prepare one or more ways to describe an object. To create a class, start with the class keyword, a name, and a body delimited by curly brackets. In the curly brackets, you can create one or more properties. After creating a class, you can declare a variable from it.

Practical Learning: Introducing Sorting

  1. Start Microsoft Visual Studio
  2. On the main menu, click File -> New -> Project...
  3. In the middle frame, click ASP.NET Web Application (.NET Framework) and change the application Name to AltairRealtors2
  4. Click OK
  5. In the New ASP.NET Web Application dialog box, click the MVC icon and click OK
  6. In the Solution Explorer, right-click Content -> Add -> Style Sheet
  7. Type Properties
  8. Click Add
  9. Change the document as follows:
    body {
        background-color: #FFFFFF;
    }
    
    .navbar-fixed-top    { background-color: #560404;  }
    .navbar-fixed-bottom { background-color: #560404;  }
    .centralizer         { margin:           auto;
                           width:            1000px;   }
    .copyright           { text-align:       center;
                           color:            cornsilk; }
    
  10. In the Solution Explorer, expand View and expand Shared
  11. Double-click Layout.cshtml to open it
  12. 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">&copy; @DateTime.Now.Year - Altair Realtors</p>
            </footer>
        </div>
    
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", required: false)
    </body>
    </html>
  13. In the Solution Explorer, expand Controllers and double-click HomeController.cs to open it
  14. Add a method as follows:
    using System.Web.Mvc;
    
    namespace AltairRealtors2.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();
            }
        }
    }
  15. In the document, right-click Listing and click Add View...
  16. In the dialog box, make sure the text box displays Listing and click Add
  17. Change the document 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 style="width: 100%" border="6">
            <tr style="background-color: #560404; color: #FFFFFF">
                <td style="width: 30px; text-align: center; color: #FFFFFF;">#</td>
                <td style="text-align: center">Category</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>Condition</td>
                <td>Bed(s)</td>
                <td>Bath(s)</td>
                <td>Garage?</td>
                <td>Stories</td>
                <td style="text-align: right">Market Value</td>
            </tr>
            @foreach (var property in houses)
            {
                if (i % 2 == 0)
                {
                    <tr style="background-color: #d3b8b8">
                        <td style="text-align: center; color: #800000;">@i</td>
                        <td style="text-align: center;">@property.PropertyType</td>
                        <td>@property.City</td>
                        <td>@property.Locality</td>
                        <td>@property.County</td>
                        <td style="text-align: center">@property.State</td>
                        <td>@property.Condition</td>
                        <td>@property.Bedrooms</td>
                        <td>@property.Bathrooms</td>
                        <td>@property.HasIndoorGarage</td>
                        <td>@property.Stories</td>
                        <td style="text-align: right">@property.MarketValue.ToString("#,000")</td>
                    </tr>
                }
                else
                {
                    <tr style="background-color: #FFFFFF">
                        <td style="text-align: center; color: #800000;">@i</td>
                        <td style="text-align: center;">@property.PropertyType</td>
                        <td>@property.City</td>
                        <td>@property.Locality</td>
                        <td>@property.County</td>
                        <td style="text-align: center">@property.State</td>
                        <td>@property.Condition</td>
                        <td>@property.Bedrooms</td>
                        <td>@property.Bathrooms</td>
                        <td>@property.HasIndoorGarage</td>
                        <td>@property.Stories</td>
                        <td style="text-align: right">@property.MarketValue.ToString("#,000")</td>
                    </tr>
                }
    
                i++;
            }
        </table>
    </div>
  18. To execute, on the main menu, click Debug -> Start Without Debugging:

    Introduction to LINQ and Classes

    Introduction to LINQ and Classes

  19. Close the browser and return to your programming environment

A Collection of Objects

A collection is a group or list of objects. To get a collection, you can create an array of items where each item is of a class type. Another technique is to use a class from a built-in .NET Framework collection-based class to create a collection of objects. After creating an array or a collection of objects, you can use the LINQ to explore that list.

Practical LearningPractical Learning: Querying an Array of Objects

  1. In the Solution Explorer, below Controllers, double-click HomeController.cs to open it
  2. 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();
            }
        }
    }
  3. 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>
  4. To execute, on the main menu, click Debug -> Start Without Debugging:

    Introduction to LINQ and Classes

    Introduction to LINQ and Classes

  5. 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);

Performing Comparisons

Practical LearningPractical Learning: Creating a Conditional Query

  1. Access the HomeController.cs tab (you can also double-click it in the Solution Explorer to open it) and change it as follows:
    using System.Web.Mvc;
    
    namespace AltairRealtors1.Controllers
    {
        public class HomeController : Controller
        {
            public Models.Property[] Properties;
    
            public HomeController()
            {
                Properties = new Models.Property[]
                {
                    . . . No Change
            }
    
            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();
            }
            
            public ActionResult ConditionalSelection()
            {
                return View();
            }
        }
    }
  2. In the document, right-click ConditionalSelection and click Add View...
  3. In the dialog box, make sure the text box displays ConditionalSelection and click Add
  4. Change the document as follows:
    @{
        ViewBag.Title = "Altair Realtors - Conditional Property Selection";
    }
    
    <h2 class="text-center">Altair Realtors - Conditional Property Selection</h2>
    
    @{
        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>
    
    @{
        if (IsPost)
        {
            if (!string.IsNullOrEmpty(Request["MarketValue"]))
            {
                string strCondition = Request["cbxConditions"];
                long marketValue = Convert.ToInt64(Request["MarketValue"]);
    
                switch (strCondition)
                {
                    case "Less Than":
                        houses = from prop in hc.Properties
                                 where prop.MarketValue < marketValue
                                 select prop;
                        break;
                    case "Less Than Or Equal To":
                        houses = from prop in hc.Properties
                                 where prop.MarketValue <= marketValue
                                 select prop;
                        break;
                    case "Equal To":
                        houses = from prop in hc.Properties
                                 where prop.MarketValue == marketValue
                                 select prop;
                        break;
                    case "Greater Than":
                        houses = from prop in hc.Properties
                                 where prop.MarketValue > marketValue
                                 select prop;
                        break;
                    case "Greater Than Or Equal T":
                        houses = from prop in hc.Properties
                                 where prop.MarketValue >= marketValue
                                 select prop;
                        break;
                    case "Different From":
                        houses = from prop in hc.Properties
                                 where prop.MarketValue != marketValue
                                 select prop;
                        break;
                    default:
                        houses = from prop
                                 in hc.Properties
                                 select prop;
                        break;
                }
            }
        }
    }
    
    @using (Html.BeginForm())
    {
        <p class="text-center">Show properties whose values are
            <select name="cbxConditions">
                <option></option>
                <option>Less Than</option>
                <option>Less Than Or Equal To</option>
                <option>Equal To</option>
                <option>Greater Than</option>
                <option>Greater Than Or Equal To</option>
                <option>Different From</option>
            </select>
            @Html.TextBox("MarketValue")
            <input type="submit" name="btnSubmit" value="Submit" />
        </p>
    
    }
    <div class="centralizer">
        <table style="width: 100%" border="6">
            <tr style="background-color: #560404; color: #FFFFFF">
                <td style="width: 30px; text-align: center; color: #FFFFFF;">#</td>
                <td style="text-align: center">Category</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>Condition</td>
                <td>Bed(s)</td>
                <td>Bath(s)</td>
                <td>Garage?</td>
                <td>Stories</td>
                <td style="text-align: right">Market Value</td>
            </tr>
            @foreach (var property in houses)
        {
            if (i % 2 == 0)
            {
                    <tr style="background-color: #d3b8b8">
                        <td style="text-align: center; color: #800000;">@i</td>
                        <td style="text-align: center;">@property.PropertyType</td>
                        <td>@property.City</td>
                        <td>@property.Locality</td>
                        <td>@property.County</td>
                        <td style="text-align: center">@property.State</td>
                        <td>@property.Condition</td>
                        <td>@property.Bedrooms</td>
                        <td>@property.Bathrooms</td>
                        <td>@property.HasIndoorGarage</td>
                        <td>@property.Stories</td>
                        <td style="text-align: right">@property.MarketValue.ToString("#,000")</td>
                    </tr>
                }
            else
            {
                    <tr style="background-color: #FFFFFF">
                        <td style="text-align: center; color: #800000;">@i</td>
                        <td style="text-align: center;">@property.PropertyType</td>
                        <td>@property.City</td>
                        <td>@property.Locality</td>
                        <td>@property.County</td>
                        <td style="text-align: center">@property.State</td>
                        <td>@property.Condition</td>
                        <td>@property.Bedrooms</td>
                        <td>@property.Bathrooms</td>
                        <td>@property.HasIndoorGarage</td>
                        <td>@property.Stories</td>
                        <td style="text-align: right">@property.MarketValue.ToString("#,000")</td>
                    </tr>
                }
    
            i++;
        }
        </table>
    </div>
  5. To execute the project, on the main menu, click Debug -> Start Without Debugging:

    Records Analysis

  6. In the left combob box, select Greater Than
  7. In the text box, type 565995

    Sorting With Class

  8. Click the Submit button:

    Sorting With Class

  9. Click the Submit button again
  10. Close the browser and return to your programming environment
for) or enumerating (using foreach

Practical Learning: Introducing LINQ

  1. Start Microsoft Visual Studio
  2. On the main menu, click File -> New -> Project...
  3. In the middle frame, click ASP.NET Web Application (.NET Framework) and change the application Name to AltairRealtors1
  4. Click OK
  5. In the New ASP.NET Web Application dialog box, click the MVC icon and click OK
  6. In the Solution Explorer, right-click Content -> Add -> Style Sheet
  7. Type Properties
  8. Click Add
  9. 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; }
  10. In the Solution Explorer, expand View and expand Shared
  11. Double-click Layout.cshtml to open it
  12. 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">&copy; @DateTime.Now.Year - Altair Realtors</p>
            </footer>
        </div>
    
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", required: false)
    </body>
    </html>
  13. In the Solution Explorer, expand Controllers and double-click HomeController.cs to open it
  14. 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();
            }
        }
    }
  15. In the document, right-click Listing and click Add View...
  16. In the dialog box, make sure the text box displays Listing and click Add

Sorting With Class

Sorting the members of a primitive-based list is quite easy. This is because the class (structures) of each data type implements the IComparable interface. This also makes it easy to sort the values of a select statement. This means that, to arrange the list of values, in the orderby statement, type the name of the from variable and use the period operator to specify the class member by which you want to arrange the list.

Practical Learning: Sorting Records

  1. Change the document 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
                     orderby prop.PropertyNumber ascending
                     select prop;
    }
    
    <h2 class="text-center">Altair Realtors - Properties Listing</h2>
    
    @{
        if (IsPost)
        {
            string strOrder = Request["cbxOrders"];
            string strColumn = Request["cbxColumns"];
    
            if (strColumn == null)
            {
                houses = from prop
                         in hc.Properties
                         orderby prop.PropertyNumber ascending
                         select prop;
            }
            else
            {
                if (strOrder == "Ascending")
                {
                    if (strColumn == "Property Type")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.PropertyType ascending
                                 select prop;
                    }
                    else if (strColumn == "City")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.City ascending
                                 select prop;
                    }
                    else if (strColumn == "County")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.County ascending
                                 select prop;
                    }
                    else if (strColumn == "State")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.State ascending
                                 select prop;
                    }
                    else if (strColumn == "Condition")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.Condition ascending
                                 select prop;
                    }
                    else if (strColumn == "YearBuilt")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.YearBuilt ascending
                                 select prop;
                    }
                    else if (strColumn == "Market Value")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.MarketValue ascending
                                 select prop;
                    }
                }
                else //  if (strOrder == "Descending")
                {
                    if (strColumn == "Property Type")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.PropertyType descending
                                 select prop;
                    }
                    else if (strColumn == "City")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.City descending
                                 select prop;
                    }
                    else if (strColumn == "County")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.County descending
                                 select prop;
                    }
                    else if (strColumn == "State")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.State descending
                                 select prop;
                    }
                    else if (strColumn == "Condition")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.Condition descending
                                 select prop;
                    }
                    else if (strColumn == "Year Built")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.YearBuilt descending
                                 select prop;
                    }
                    else if (strColumn == "Market Value")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.MarketValue descending
                                 select prop;
                    }
                }
            }
        }
    }
    
    @using (Html.BeginForm())
    {
        <p class="text-center">Sort by: <select name="cbxColumns">
                <option></option>
                <option>Property Type</option>
                <option>City</option>
                <option>County</option>
                <option>State</option>
                <option>Condition</option>
                <option>Year Built</option>
                <option>Market Value</option>
            </select> in
            <select name="cbxOrders">
                <option></option>
                <option>Ascending</option>
                <option>Descending</option>
            </select>
            <input type="submit" name="btnSubmit" value="Submit" />
        </p>
    
    }
    
    <div class="centralizer">
        <table style="width: 100%" border="6">
            <tr style="background-color: #560404; color: #FFFFFF">
                <td style="width: 30px; text-align: center; color: #FFFFFF;">#</td>
                <td style="text-align: center">Category</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>Condition</td>
                <td>Bed(s)</td>
                <td>Bath(s)</td>
                <td>Garage?</td>
                <td>Stories</td>
                <td style="text-align: right">Market Value</td>
            </tr>
            @foreach (var property in houses)
            {
                if (i % 2 == 0)
                {
                    <tr style="background-color: #d3b8b8">
                        <td style="text-align: center; color: #800000;">@i</td>
                        <td style="text-align: center;">@property.PropertyType</td>
                        <td>@property.City</td>
                        <td>@property.Locality</td>
                        <td>@property.County</td>
                        <td style="text-align: center">@property.State</td>
                        <td>@property.Condition</td>
                        <td>@property.Bedrooms</td>
                        <td>@property.Bathrooms</td>
                        <td>@property.HasIndoorGarage</td>
                        <td>@property.Stories</td>
                        <td style="text-align: right">@property.MarketValue.ToString("#,000")</td>
                    </tr>
                }
                else
                {
                    <tr style="background-color: #FFFFFF">
                        <td style="text-align: center; color: #800000;">@i</td>
                        <td style="text-align: center;">@property.PropertyType</td>
                        <td>@property.City</td>
                        <td>@property.Locality</td>
                        <td>@property.County</td>
                        <td style="text-align: center">@property.State</td>
                        <td>@property.Condition</td>
                        <td>@property.Bedrooms</td>
                        <td>@property.Bathrooms</td>
                        <td>@property.HasIndoorGarage</td>
                        <td>@property.Stories</td>
                        <td style="text-align: right">@property.MarketValue.ToString("#,000")</td>
                    </tr>
                }
    
                i++;
            }
        </table>
    </div>
  2. To execute the project, on the main menu, click Debug -> Start Without Debugging:

    Sorting With Class

  3. In the Sort by combo box, select City
  4. In the In combo box, select Ascending

    Sorting With Class

  5. Click the Submit button:

    Sorting With Class

    Sorting With Class

  6. In the left combo box, select State
  7. In the right combo box, select Descending
  8. Click the Submit button:

    Sorting With Class

  9. Close the browser and return to your programming environment

To create a query, you write a statement using words and operators of the LINQ. The most fundamental operation you can perform on the LINQ consists of creating, also referred to as selecting, or querying, a list of values, from an existing list. The list can be an array of 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 };
}
</body>
</html>

The basic formula to query a list is:

var variable-name = from value-holder in original-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 variable-name is a name of a 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.

The original-list represents the name of the variable that you would have declared already. The original-list can be an array of 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;

Practical LearningPractical Learning: Creating a Query

  1. Change the document as follows:
  2. To execute the project, on the main menu, click Debug -> Start Without Debugging:

    Introduction to LINQ

  3. Close the browser and return to your programming environment
  4. 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>
  5. To execute the project, on the main menu, click Debug -> Start Without Debugging:

    Introduction to LINQ

  6. Close the browser and return to your programming environment
  7. 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>
  8. To execute the project, on the main menu, click Debug -> Start Without Debugging:

    Introduction to LINQ

  9. Close the browser and return to your programming environment
  10. 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>
  11. To execute the project, on the main menu, click Debug -> Start Without Debugging:

    Introduction to LINQ

  12. Close the browser and return to your programming environment
  13. In the Solution Explorer, right-click Models -> Add -> Class...
  14. Type Property as the name of the file
  15. Press Enter
  16. 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 the LINQ and Classes

Practical Learning: Ending the Lesson

Practical Learning: Sorting Records

  1. Change the document 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
                     orderby prop.PropertyNumber ascending
                     select prop;
    }
    
    <h2 class="text-center">Altair Realtors - Properties Listing</h2>
    
    @{
        if (IsPost)
        {
            string strOrder = Request["cbxOrders"];
            string strColumn = Request["cbxColumns"];
    
            if (strColumn == null)
            {
                houses = from prop
                         in hc.Properties
                         orderby prop.PropertyNumber ascending
                         select prop;
            }
            else
            {
                if (strOrder == "Ascending")
                {
                    if (strColumn == "Property Type")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.PropertyType ascending
                                 select prop;
                    }
                    else if (strColumn == "City")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.City ascending
                                 select prop;
                    }
                    else if (strColumn == "County")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.County ascending
                                 select prop;
                    }
                    else if (strColumn == "State")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.State ascending
                                 select prop;
                    }
                    else if (strColumn == "Condition")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.Condition ascending
                                 select prop;
                    }
                    else if (strColumn == "YearBuilt")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.YearBuilt ascending
                                 select prop;
                    }
                    else if (strColumn == "Market Value")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.MarketValue ascending
                                 select prop;
                    }
                }
                else //  if (strOrder == "Descending")
                {
                    if (strColumn == "Property Type")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.PropertyType descending
                                 select prop;
                    }
                    else if (strColumn == "City")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.City descending
                                 select prop;
                    }
                    else if (strColumn == "County")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.County descending
                                 select prop;
                    }
                    else if (strColumn == "State")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.State descending
                                 select prop;
                    }
                    else if (strColumn == "Condition")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.Condition descending
                                 select prop;
                    }
                    else if (strColumn == "Year Built")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.YearBuilt descending
                                 select prop;
                    }
                    else if (strColumn == "Market Value")
                    {
                        houses = from prop in hc.Properties
                                 orderby prop.MarketValue descending
                                 select prop;
                    }
                }
            }
        }
    }
    
    @using (Html.BeginForm())
    {
        <p class="text-center">Sort by: <select name="cbxColumns">
                <option></option>
                <option>Property Type</option>
                <option>City</option>
                <option>County</option>
                <option>State</option>
                <option>Condition</option>
                <option>Year Built</option>
                <option>Market Value</option>
            </select> in
            <select name="cbxOrders">
                <option></option>
                <option>Ascending</option>
                <option>Descending</option>
            </select>
            <input type="submit" name="btnSubmit" value="Submit" />
        </p>
    
    }
    
    <div class="centralizer">
        <table style="width: 100%" border="6">
            <tr style="background-color: #560404; color: #FFFFFF">
                <td style="width: 30px; text-align: center; color: #FFFFFF;">#</td>
                <td style="text-align: center">Category</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>Condition</td>
                <td>Bed(s)</td>
                <td>Bath(s)</td>
                <td>Garage?</td>
                <td>Stories</td>
                <td style="text-align: right">Market Value</td>
            </tr>
            @foreach (var property in houses)
            {
                if (i % 2 == 0)
                {
                    <tr style="background-color: #d3b8b8">
                        <td style="text-align: center; color: #800000;">@i</td>
                        <td style="text-align: center;">@property.PropertyType</td>
                        <td>@property.City</td>
                        <td>@property.Locality</td>
                        <td>@property.County</td>
                        <td style="text-align: center">@property.State</td>
                        <td>@property.Condition</td>
                        <td>@property.Bedrooms</td>
                        <td>@property.Bathrooms</td>
                        <td>@property.HasIndoorGarage</td>
                        <td>@property.Stories</td>
                        <td style="text-align: right">@property.MarketValue.ToString("#,000")</td>
                    </tr>
                }
                else
                {
                    <tr style="background-color: #FFFFFF">
                        <td style="text-align: center; color: #800000;">@i</td>
                        <td style="text-align: center;">@property.PropertyType</td>
                        <td>@property.City</td>
                        <td>@property.Locality</td>
                        <td>@property.County</td>
                        <td style="text-align: center">@property.State</td>
                        <td>@property.Condition</td>
                        <td>@property.Bedrooms</td>
                        <td>@property.Bathrooms</td>
                        <td>@property.HasIndoorGarage</td>
                        <td>@property.Stories</td>
                        <td style="text-align: right">@property.MarketValue.ToString("#,000")</td>
                    </tr>
                }
    
                i++;
            }
        </table>
    </div>
  2. To execute the project, on the main menu, click Debug -> Start Without Debugging:

    Sorting With Class

  3. In the Sort by combo box, select City
  4. In the In combo box, select Ascending

    Sorting With Class

  5. Click the Submit button:

    Sorting With Class

    Sorting With Class

  6. In the left combo box, select State
  7. In the right combo box, select Descending
  8. Click the Submit button:

    Sorting With Class

  9. Close the browser and return to your programming environment
@{
    string[] elements = new string[] { "Hydrogen", "Helium", "Lithium",
                                       "Beryllium", "Boron", "Carbon" };

    var atoms = from elm in elements
                orderby elm ascending
                select elm;
}

Home Copyright © 2008-2018, FunctionX, Inc. Next