Linq中常用语法

Posted ypyp123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linq中常用语法相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataBaseFirst
{
class Program
{
static void Main(string[] args)
{
StuDBEntities1 stuDBEntities = new StuDBEntities1();

////-----------------新增数据到数据库
//List<students> student = new List<students>();
//students stu = new students();
//stu.sno = "120";
//stu.ssex = "女";
//stu.sname = "周宇菲11169";
//stu.sbirthday = Convert.ToDateTime("2020-05-03");
//stu.@class = "95035";
//插入单条数据
//stuDBEntities.students.Add(stu);
//stuDBEntities.SaveChanges();

//students tu = new students()
//{
// sno = "119",
// ssex = "男",
// sname = "周宇菲156",
// sbirthday = Convert.ToDateTime("2020-05-03"),
// @class = "95065"
// };
////多条数据插入
//student.Add(stu);
//student.Add(tu);
//stuDBEntities.students.AddRange(student);
//stuDBEntities.SaveChanges();

//-----------------根据学号删除数据
//students stu1 = new students();
//stu1.sno = "113";
//stuDBEntities.Entry<students>(stu1).State = System.Data.Entity.EntityState.Deleted;
//stuDBEntities.SaveChanges();

//-----------------根据id编辑数据
//students stu1 = new students();
//stu1.sno = "112";
//stu1.sname = "周宇菲1110";
//stu1.ssex = "女";
//stu1.@class = "95025";
//stu1.sbirthday = Convert.ToDateTime("2020-05-03");
//stuDBEntities.Entry<students>(stu1).State = System.Data.Entity.EntityState.Modified;
//stuDBEntities.SaveChanges();

//---------------------查询数据
//List<students> stu = stuDBEntities.students.ToList();
//foreach (students tu in stu)
//{
// Console.WriteLine(tu.sname);
//}
//Console.ReadKey();

//---------------------带条件的数据查询
//List<students> stu = stuDBEntities.students.Where(u => u.sno == "101").ToList();
//foreach (students tu in stu)
//{
// Console.WriteLine(tu.sname);
//}
//Console.ReadKey();


//---------------------查询排序 正序/倒叙
//List<students> stu = stuDBEntities.students.OrderBy(t => t.sbirthday).ToList();正序
//List<students> stu = stuDBEntities.students.OrderBy(t => t.sbirthday).OrderByDescending(t=>t.sbirthday).ToList();//倒序
//foreach (students tu in stu)
//{
// Console.WriteLine(tu.sbirthday);
//}
//Console.ReadKey();

//------------------Linq查询
//var student = from stu in stuDBEntities.students where stu.sno == "101" select stu;
//---------------------linq+lamda表达式查询数据
//List<students> student1 = (from stu in stuDBEntities.students where stu.sno == "101" select stu).ToList();
//List<students> student2 = student.Where(u => u.sname == "王丽").ToList();

//---------------------var 关键字
//linq擦寻首先包含一个变量,一包含哈寻的结果集,通常使用var来声明

//---------------------使用Linq多表联查
//var teacher_course = from te in stuDBEntities.teachers
// join cu in stuDBEntities.courses
// on te.tno equals cu.tno
// orderby te.tno
// select cu;

//使用Linq查询加函数查询两种写法
//var student = (from stu in stuDBEntities.students select stu).Count();
//var student1 = stuDBEntities.students.Count();

//var Minstudent = (from stu in stuDBEntities.students select stu).Min();
//var Min1student1 = stuDBEntities.students.Min();

//求余函数
// var Min1student1 = (from stu in stuDBEntities.students select stu).Skip(2);

//----------------Linq查询分页
//List<students> stude = (from stu in stuDBEntities.students orderby stu.sno descending select stu).Skip(2).Take(5).ToList();
//List<students> stude1 = stuDBEntities.students.OrderByDescending(stu => stu.sno).Skip(2).Take(5).ToList();
//foreach (students tu in stude1)
//{
// Console.WriteLine(tu.sname);
//}
//Console.ReadKey();

//---------------Linq的模糊查询Contains 相当于like
//linq写法
// List<students> stude = (from stu in stuDBEntities.students where stu.sname.Contains("宇") select stu).ToList();
////lamda写法
//List<students> stude = stuDBEntities.students.Where(tu => tu.sname.Contains("宇")).OrderByDescending(tu => tu.sno).ToList();
//foreach (students tu in stude)
//{
// Console.WriteLine(tu.sname);
//}
//Console.ReadKey();

//分组查询取单列
//var stude1 = from stu in stuDBEntities.students
// group stu by stu.sname into n
// select new
// {
// n.Key
// };
//foreach (var item in stude1)
//{
// Console.WriteLine(item.Key);
//}
//Console.ReadKey();
//取多列
//var stude1 = from stu in stuDBEntities.students
// group stu by new { stu.sname, stu.ssex } into n
// select new
// {
// n.Key.sname,
// n.Key.ssex
// };
//foreach (var item in stude1)
//{
// Console.WriteLine(item.sname);
//}
//Console.ReadKey();

//使用Lamda写groupby
//var stude1 = stuDBEntities.students.GroupBy(p => p.sno).ToList();
//foreach (var item in stude1)
//{
// Console.WriteLine(item.Key);
//}
////多列
//var stude2= stuDBEntities.students.GroupBy(p => new { p.sname, p.ssex }).ToList();
//foreach (var item in stude2)
//{
// Console.WriteLine(item.Key.sname);
//}
//Console.ReadKey();
//--------------------Linq中的in用法

//var stude1 = from stu in stuDBEntities.students where (new string[] { "101","103" }).Contains(stu.sno) select stu;
//foreach (var item in stude1)
//{
// Console.WriteLine(item.sname);
//}
//Console.ReadKey();

//var info = from stu in stuDBEntities.STUDENT where (new int?[] { 1, 2 }).Contains(stu.s_code) select stu;
//foreach (var item in info)
//{
// Console.WriteLine(item.s_address);
//}
//Console.ReadKey();

//Linq中的sql
//var stu = stuDBEntities.students.SqlQuery("select * from students where sno in(‘101‘,‘102‘)");
//foreach (var item in stu)
//{
// Console.WriteLine(item.sname);
//}
//Console.ReadKey();
}
}
}

以上是关于Linq中常用语法的主要内容,如果未能解决你的问题,请参考以下文章

常用linq语法 - 孔小爽 - 博客园

es6新增在vue中常用语法

LINQ基础

linq语法大全(转集)

Linq 语法

LINQ