LINQ

一. Where  :选择行.

 

隐藏行号 复制代码 这是一段程序代码。
  1. IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
    

 

 IEnumerable<TSource> Where<TSource>(f => 一个条件表达式)
隐藏行号 复制代码 这是一段程序代码。
  1. public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, int count);
    

 

Take : 选择行

a.task(5)  //选择前5行.

例:

List.Where(f => typeof(Csla.Core.IPropertyInfo).IsAssignableFrom(f.FieldType))

 

红色部分既可以是一条语句,也可以是一段代码块{…},代码块里面有一个返回值. List中有过少个元素.

则这条语句/代码块就执行多少此.

 

 

里面的表达式不不会立即执行,只是什么时候需要使用返回值的时候才执行ladm表达式.

 

 

 

隐藏行号 复制代码 这是一段程序代码。
  1. static void Main(string[] args)
    
  2. {
    
  3.     List<int> nList = new List<int>();
    
  4.     IEnumerable<int> sFilter;
    
  5.     int nCount;
    
  6.     nList.Add(1);
    
  7.     nList.Add(2);
    
  8.     nList.Add(3);
    
  9.     nList.Add(4);
    
  10.     nList.Add(5);
    
  11.     nList.Add(6);
    
  12.     nList.Add(7);
    
  13.     nList.Add(8);
    
  14.     nList.Add(9);
    
  15.     nList.Add(10);
    
  16.     nList.Add(11);
    
  17.     sFilter = nList.Where(f => 
    
  18.         {  //有多少个元素,这个代码块就执行多少此.
    
  19.          if (f > 5)
    
  20.          {
    
  21.              if (f.Equals(10))
    
  22.              {
    
  23.                  return true;
    
  24.              }
    
  25.          }
    
  26.          else
    
  27.          {
    
  28.              return false;
    
  29.          }
    
  30.          return false;
    
  31.          });
    
  32.     nCount = sFilter.Count();   //这里才开始执行Where中的代码块.
    
  33. 
    
  34. }
    

 

二.Select  //选择列.

 

隐藏行号 复制代码 这是一段程序代码。
  1. public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
    

 

 

 

 

 

IEnumerable<CSLAPropertyInfo>.Select(f=>{代码块…})

 

 

 

 

隐藏行号 复制代码 这是一段程序代码。
  1. private static IEnumerable<CSLAPropertyInfo> LoadFromType(Type type)
    
  2. {
    
  3.     var staticFields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
    
  4.     return staticFields.Where(f => typeof(Csla.Core.IPropertyInfo).IsAssignableFrom(f.FieldType))
    
  5.         .Select(f =>
    
  6.         {
    
  7.             bool isChild = false;
    
  8.             var property = f.GetValue(null) as Csla.Core.IPropertyInfo;
    
  9.             //判断是不是孩子对象
    
  10.             var fieldType = f.FieldType;
    
  11.             if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(PropertyInfo<>))
    
  12.             {
    
  13.                 var argType = fieldType.GetGenericArguments()[0];
    
  14.                 while (argType.BaseType != null)
    
  15.                 {
    
  16.                     if (argType.IsGenericType && argType.GetGenericTypeDefinition() == typeof(GBusinessListBase<,>))
    
  17.                     {
    
  18.                         isChild = true;
    
  19.                         break;
    
  20.                     }
    
  21.                     argType = argType.BaseType;
    
  22.                 }
    
  23.             }
    
  24.             return new CSLAPropertyInfo(property, isChild);
    
  25.         });
    
  26. }
    

 

 

posted @   SouthAurora  Views(284)  Comments(0Edit  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示