LINQ Archives - Tech Insights Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Wed, 22 Nov 2023 10:55:30 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 LINQ Archives - Tech Insights 32 32 230003556 LINQ: Sort Multiple Columns using LINQ in C# https://reactconf.org/linq-sorting-multiple-columns-using-linq-in-csharp/ https://reactconf.org/linq-sorting-multiple-columns-using-linq-in-csharp/#respond Wed, 22 Nov 2023 10:55:30 +0000 https://labpys.com/?p=2183 In this article, we will learn How to Sort multiple Columns using LINQ in C# using Order by , ThenBy, OrderbyDescending, and ThenbyDescending methods. Here’s an example of how to …

The post LINQ: Sort Multiple Columns using LINQ in C# appeared first on Tech Insights.

]]>
In this article, we will learn How to Sort multiple Columns using LINQ in C# using Order by , ThenBy, OrderbyDescending, and ThenbyDescending methods.

Here’s an example of how to Sort multiple Columns using LINQ in C#.

Create an Asp.NET Core MVC or Web API Project

Assuming you have Visual Studio 2019 or Visual Studio 2022 installed, follow these steps to create a new ASP.NET Core Project

  • Open the visual studio (IDE).
  • Click on the “Create new project” option.
  • Choose “ASP.NET Core Web Application” from the list of available templates.
  • Click the Next button.
  • The configure a new project, specify the name and location of your project.
  • Click the “Next” button.
  • Then select .Net Core as the runtime and choose the version from the dropdown list at the top.
  • Make sure to uncheck the checkboxes for “Enable Docker Support” and “Configure for HTTPS” since we won’t be using authentication.
  • Click the “Create” button

Consider an example Employee class with properties like FirstName, LastName, City, and State. We’ll sort employees based on their FirstName and LastName.

Create a Model

namespace WEB_API_without_EF.Models
{
    public class Employees
    {
        public int ID { get; set; }
        public string Company { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string JobTitle { get; set; }
        public string Address { get; set;}

        public string City { get; set; }

        public string state { get; set; }

    }
}

Create a Controller

namespace WEB_API_without_EF.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    { 
[HttpGet("GetOrderBy")]
        public IActionResult GetOrderBy()
        {
            List<Employees> employees = new List<Employees>
            {
                new Employees{FirstName="Anna", LastName="Bedecs", Company="Company A", City="Seattle",state="WA"},
                new Employees{FirstName="Antonio", LastName="Gratacos Solsona", Company="Company B", City="Boston",state="MA"},
                new Employees{FirstName="Thomas" ,LastName="Axen", Company="Company C", City="Seattle",state="WA"},
                new Employees{FirstName="Christina", LastName="Lee", Company="Company D", City="New York",state="NY"},
                new Employees{FirstName="Martin" ,LastName="O’Donnell", Company="Company E", City="Minneapolis",state="MN"},
                new Employees{FirstName="Francisco", LastName="Pérez-Olaeta", Company="Company F", City="Milwaukee",state="WI"},

            };

            //Order by Ascending Order

            var OrderbyEmployeeAsc = employees.
                        OrderBy(x => x.FirstName)
                        .ThenBy(x => x.LastName)
                        .ToList();
            // Descending Order

            var OrderbyEmployeeDesc = employees.
                        OrderBy(x => x.LastName)
                        .ThenByDescending(x => x.FirstName)
                        .ToList();

            

            return Ok(OrderbyEmployeeAsc);
        }
}
}

 Always utilize ThenBy() after OrderBy() because OrderBy() generates an IOrderedEnumerable object which then exposes the methods ThenBy() and ThenByDescending(). This means that we can order by multiple columns.

Output

Sorting Multiple Columns using LINQ  in C#

See More:

Sorting Multiple Columns using LINQ  in C#

The post LINQ: Sort Multiple Columns using LINQ in C# appeared first on Tech Insights.

]]>
https://reactconf.org/linq-sorting-multiple-columns-using-linq-in-csharp/feed/ 0 2183
Group by Multiple Columns in LINQ C# https://reactconf.org/how-to-group-by-multiple-columns-in-linq-csharp/ https://reactconf.org/how-to-group-by-multiple-columns-in-linq-csharp/#respond Wed, 22 Nov 2023 07:35:55 +0000 https://labpys.com/?p=2167 In this article, we will learn the Group y multiple Columns in LINQ C#. The ‘group by ‘method in LINQ to group data based on one or more columns. Here’s …

The post Group by Multiple Columns in LINQ C# appeared first on Tech Insights.

]]>
In this article, we will learn the Group y multiple Columns in LINQ C#. The ‘group by ‘method in LINQ to group data based on one or more columns.

Here’s an example of how to Group by multiple columns using LINQ in C#.

Create an Asp.NET Core MVC or Web API Project

Assuming you have Visual Studio 2019 or Visual Studio 2022 installed, follow these steps to create a new ASP.NET Core Project

  • Open the visual studio (IDE).
  • Click on the “Create new project” option.
  • Choose “ASP.NET Core Web Application” from the list of available templates.
  • Click the Next button.
  • The configure a new project, specify the name and location of your project.
  • Click the “Next” button.
  • Then select .Net Core as the runtime and choose the version from the dropdown list at the top.
  • Make sure to uncheck the checkboxes for “Enable Docker Support” and “Configure for HTTPS” since we won’t be using authentication.
  • Click the “Create” button

Consider an example Employee class with properties like FirstName, LastName, City, and State. We’ll group employees based on their City and State.

Create a Model

namespace WEB_API_without_EF.Models
{
    public class Employees
    {
        public int ID { get; set; }
        public string Company { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string JobTitle { get; set; }
        public string Address { get; set;}

        public string City { get; set; }

        public string state { get; set; }

    }
}

Create a Controller


namespace WEB_API_without_EF.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {

        [HttpGet("GetgroupBy")]
        public IActionResult GetgroupBy()
        {
            List<Employees> employees = new List<Employees>
            {
                new Employees{FirstName="Anna", LastName="Bedecs", Company="Company A", City="Seattle",state="WA"},
                new Employees{FirstName="Antonio", LastName="Gratacos Solsona", Company="Company B", City="Boston",state="MA"},
                new Employees{FirstName="Thomas" ,LastName="Axen", Company="Company C", City="Seattle",state="WA"},
                new Employees{FirstName="Christina", LastName="Lee", Company="Company D", City="New York",state="NY"},
                new Employees{FirstName="Martin" ,LastName="O’Donnell", Company="Company E", City="Minneapolis",state="MN"},
                new Employees{FirstName="Francisco", LastName="Pérez-Olaeta", Company="Company F", City="Milwaukee",state="WI"},

            };

            var groupemployee = from emp in employees
                                group emp by new { emp.City, emp.state } into empgroup
                                select new
                                {
                                   Name =$"City : {empgroup.Key.City} State : {empgroup.Key.state}",
                                   employees= empgroup.ToList(),
                                };
            
      

            return Ok(groupemployee);

        }
}
}

Output:

Group by Multiple Columns in LINQ C#

See More :

Upload Multiple Files in Angular with ASP.NET Core Web API

The post Group by Multiple Columns in LINQ C# appeared first on Tech Insights.

]]>
https://reactconf.org/how-to-group-by-multiple-columns-in-linq-csharp/feed/ 0 2167
ASP.NET Core MVC | Populate DropdownList with Month Names Using LINQ https://reactconf.org/asp-net-core-mvc-populate-dropdownlist-with-month-names-using-linq/ https://reactconf.org/asp-net-core-mvc-populate-dropdownlist-with-month-names-using-linq/#respond Sun, 23 Apr 2023 00:35:00 +0000 https://labpys.com/?p=1154 A dropdown list is a commonly used control in ASP.NET Core MVC for allowing users to select an option from a list of things. Frequently, the dropdown list control’s list …

The post ASP.NET Core MVC | Populate DropdownList with Month Names Using LINQ appeared first on Tech Insights.

]]>
A dropdown list is a commonly used control in ASP.NET Core MVC for allowing users to select an option from a list of things. Frequently, the dropdown list control’s list of items includes a list of months from which the user can choose. Manually populating a dropdown list control with month names, on the other hand, might be time-consuming and inconvenient. In this post, we’ll look at how to use ASP.NET Core MVC | Populate DropdownList with Month Names Using LINQ.

Prerequisites

You will need the following to follow along with this tutorial:

  • NET Core SDK from here
  • Visual Studio or Visual Studio Code from here

Steps for ASP.NET Core MVC | Populate DropdownList with Month Names Using LINQ

Create a Dropdownlist control.

First, in our Razor view, we need to add a Dropdownlist control.

@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "Home Page";
}

<div class="row">
    <div class="col-md-4">
        <div class="form-group">
            <label class="control-label">Select Month</label>
            <select nme="getMonth" class="form-control" asp-items="@ViewBag.Month"></select>

        </div>
    </div>
</div>
ASP.NET Core MVC | Populate DropdownList with Month Names Using LINQ

Create a List of Month Names

The next step is to make a list of month names. We’ll use LINQ to generate a list of months, with the current month set as the default value.

 public IActionResult Index()
        {
          var GetMonth =  Enumerable.Range(1,12)
                .Select(m => new SelectListItem
                {
                    Text = new DateTimeFormatInfo().GetAbbreviatedMonthName(m),
                    Value = m.ToString()
                }).ToList();
                       
            ViewBag.Month = GetMonth; 
            return View();
        }
Document

Conclusion

Using LINQ to populate a Dropdownlist control with month names in ASP.NET Core is a simple process. You can rapidly construct a Dropdownlist control with a list of months for your users to select by following the methods explained in this article.

FAQs

  1. What exactly is LINQ?

LINQ is an acronym that stands for Language Integrated Query. It is a.NET language feature that allows developers to query several data sources using a uniform syntax.

2. Is it possible to associate the Dropdownlist control with a list of objects?

Yes, you may associate the Dropdownlist control with a collection of objects. Simply build a list of SelectListItem objects, where the Value property is the object’s ID and the Text property is the object’s name.

The post ASP.NET Core MVC | Populate DropdownList with Month Names Using LINQ appeared first on Tech Insights.

]]>
https://reactconf.org/asp-net-core-mvc-populate-dropdownlist-with-month-names-using-linq/feed/ 0 1154