ASP.NET中LINQ的基本用法
作者:互联网
此Demo只是一个极其简单的LINQ查询Demo
一个类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class NBA_Star
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Champion { get; set; }
}
}
主类的代码LINQ的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var nba_stars = new List<NBA_Star> {
new NBA_Star{ FirstName="迈克尔", LastName="乔丹", Champion=6},
new NBA_Star{ FirstName="蒂姆", LastName="邓肯", Champion=5},
new NBA_Star{ FirstName="勒布朗", LastName="詹姆斯", Champion=3},
new NBA_Star{ FirstName="史蒂芬 ", LastName="库里", Champion=3},
new NBA_Star{ FirstName="史蒂夫 ", LastName="纳什", Champion=0}
};
//查询语法查询出,至少得到5个总冠的球星
var starts = from star in nba_stars
where star.Champion >= 5
select new
{
Name = star.FirstName + "." + star.LastName,
Champion = star.Champion
};
Console.WriteLine("至少五个总冠军的球星");
foreach (var item in starts)
{
Console.WriteLine($"姓名:{item.Name},总冠军:{item.Champion}");
}
Console.WriteLine();
//查找至少得到5个总冠的球星的Lambda
var starts2 = nba_stars.Where(s => s.Champion >= 5).Select(s => new
{
Name = s.FirstName + "." + s.LastName,
Champion = s.Champion
});
foreach (var item in starts2)
{
Console.WriteLine($"姓名:{item.Name},总冠军:{item.Champion}");
}
Console.WriteLine();
//五个总冠军的人数
var starts3 = nba_stars.Where(s => s.Champion >= 5).Count();
Console.WriteLine("至少得到5个总冠的球星人数"+starts3);
Console.WriteLine();
//最后一个冠军
var last_star = nba_stars.LastOrDefault();
Console.WriteLine("最后一个夺冠球星"+last_star.FirstName+"."+last_star.LastName);
Console.WriteLine();
//得到六冠的球星是
var Six_Star = nba_stars.Select(ss => new
{
Name = ss.FirstName + "." + ss.LastName,
Champion = ss.Champion
}).FirstOrDefault(s => s.Champion == 6);
Console.WriteLine($"得到6冠的球星是{Six_Star.Name}");
Console.WriteLine();
}
}
}
也是为了方便以后用到找不到的时候,可以来自己博客查看
标签:ASP,Console,Champion,FirstName,System,LINQ,WriteLine,using,NET 来源: https://blog.csdn.net/a1439775520/article/details/105867206