LINQ
一. Where :選擇行.
IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
IEnumerable<TSource> Where<TSource>(f => 一個條件表達(dá)式)
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中有過少個元素.
則這條語句/代碼塊就執(zhí)行多少此.
里面的表達(dá)式不不會立即執(zhí)行,只是什么時候需要使用返回值的時候才執(zhí)行l(wèi)adm表達(dá)式.
static void Main(string[] args)
{List<int> nList = new List<int>();
IEnumerable<int> sFilter;
int nCount;nList.Add(1);
nList.Add(2);
nList.Add(3);
nList.Add(4);
nList.Add(5);
nList.Add(6);
nList.Add(7);
nList.Add(8);
nList.Add(9);
nList.Add(10);
nList.Add(11);
sFilter = nList.Where(f =>
{ //有多少個元素,這個代碼塊就執(zhí)行多少此.if (f > 5)
{if (f.Equals(10)){return true;}
}
else{return false;}
return false;});
nCount = sFilter.Count(); //這里才開始執(zhí)行Where中的代碼塊.}
二.Select //選擇列.
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
IEnumerable<CSLAPropertyInfo>.Select(f=>{代碼塊…})
private static IEnumerable<CSLAPropertyInfo> LoadFromType(Type type)
{var staticFields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
return staticFields.Where(f => typeof(Csla.Core.IPropertyInfo).IsAssignableFrom(f.FieldType))
.Select(f =>
{bool isChild = false;
var property = f.GetValue(null) as Csla.Core.IPropertyInfo;
//判斷是不是孩子對象var fieldType = f.FieldType;
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(PropertyInfo<>))
{var argType = fieldType.GetGenericArguments()[0];while (argType.BaseType != null)
{if (argType.IsGenericType && argType.GetGenericTypeDefinition() == typeof(GBusinessListBase<,>))
{isChild = true;break;}
argType = argType.BaseType;
}
}
return new CSLAPropertyInfo(property, isChild);
});
}

浙公網(wǎng)安備 33010602011771號