- public class LinqShow
- {
- ///
- /// Linq to object(数组、集合)--内存里面的数据
- /// Linq to sql(查询数据库用的)--在数据库数据
- /// Linq XML查询XML文件
- ///
- List
studentsList = new List() - {
- new Students
- {
- Id=1,
- Name="喜刷刷",
- ClassId=2,
- Age=22
- },
- new Students
- {
- Id=2,
- Name="喜刷刷2",
- ClassId=2,
- Age=22
- },
- new Students
- {
- Id=3,
- Name="喜刷刷3",
- ClassId=2,
- Age=22
- },
- new Students
- {
- Id=4,
- Name="喜刷刷4",
- ClassId=2,
- Age=22
- },
- new Students
- {
- Id=5,
- Name="喜刷刷3",
- ClassId=2,
- Age=22
- }
- };
-
-
- List
stuClasses = new List - {
- new StuClass
- {
- ID=1,
- Name="111"
- },
- new StuClass
- {
- ID=2,
- Name="222"
- },
- new StuClass
- {
- ID=3,
- Name="333"
- },
- new StuClass
- {
- ID=4,
- Name="444"
- }
- };
-
-
- public void Show()
- {
- List
list = new List(); - #region
- foreach (var item in studentsList)
- {
- if (item.ClassId == 2)
- {
- list.Add(item);
- }
- }
- //自定义Linq
- list = AntWhere(studentsList, s => s.Age < 23);
- //官方Linq
- var list4 = studentsList.Where(s => s.Age < 23);
- //自定义扩展Linq
- var list7 = studentsList.AntWhere(s => s.Age < 23);
- //表达式方法(匿名类)将查询的结果一新的匿名类返回
- var list5 = from s in studentsList
- where s.Age == 22
- select new
- {
- Name = s.Name
- };
- var list6 = studentsList.Join(stuClasses, s => s.ClassId, c => c.ID, (s, c) => new
- {
- ID = c.ID,
- Name = c.Name
- });
- #endregion
-
- //EF框架简单使用
-
- OAEntities db = new OAEntities();
- var userInfoList = db.UserInfo.Where(s => s.ID > 1);
-
- foreach (var item in userInfoList)
- {
- Console.WriteLine(item.Remark);
- }
- }
-
- ///
- /// Func
Students委托形参,bool委托返回值类型 - ///
- ///
- ///
- ///
- public List
AntWhere(List resource,Funcbool>where) - {
- List
list = new List(); - foreach (var item in resource)
- {
- if (where.Invoke(item))
- {
- list.Add(item);
- }
- }
- return list;
- }
-
-
- }
-
- ///
- /// 自定义扩展Linq方法
- ///
- public static class LinqExtend
- {
- public static IEnumerable<T> AntWhere<T>(this IEnumerable
resource, Funcbool > where) - {
- List
list = new List(); - foreach (var item in resource)
- {
- if (where.Invoke(item))
- {
- list.Add(item);
- }
- }
- return list;
- }
- }