Linq 中的Aggregate一些用法
Linq to Entities中使用Aggregate语句时,需要先将数据从数据库中需要使用AsEnumerable()方法将数据从数据库中获取出来,以便于操作数据,否则Aggregate语句中的string.Format、三元运算符等无法识别。
下面是使用Aggregate将多行结果返回成一个字符串的语句:
1 string emergencyContacts = 2 entitis.PilotEmergencyContactInfoRecord.Where( 3 t => t.WasincJobApplicationId == wasincJobApplicationId && t.IsDel == false) 4 .AsEnumerable() 5 .Aggregate(string.Empty, 6 (current, next) => 7 string.Format("{0},{1}-{2}-{3}", current, next.ContactName, next.CellPhone, next.HomePhoneNumber), 8 result => result.Length > 2 ? result.Substring(2) : string.Empty);
--------------------------------------------------
-猫不在口袋里不要谈猫.