Monday, April 19, 2021

LINQ List

ASP.NET Core LINQ Demo for List

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LINQArray.Controllers
{
    public class HomeController : Controller
    {
        public async Task Index()
        {
            List fruits = new List()
            {
                new Fruit{ Id=1, Name="Apple",Price=23, Units=9},
                new Fruit{ Id=2, Name="Guava",Price=13, Units=4},
                new Fruit{ Id=3, Name="Orange",Price=22, Units=7},
                new Fruit{ Id=4, Name="Pear",Price=25, Units=8},
            };
            IEnumerable _fruits = from f in fruits select f;
            foreach (var fruit in _fruits)
            {
               await Response.WriteAsync(fruit.Id.ToString() + ", " + fruit.Name + ", " + fruit.Price.ToString() + ", " + fruit.Units.ToString() + "
"); } return string.Empty; } } class Fruit { public int Id { get; set; } public string Name { get; set; } public int Price { get; set; } public int Units { get; set; } } }

Updated on 6 July 2023

No comments:

Post a Comment

Hot Topics