foreach 与 Linq的 Select 效率问题】的更多相关文章

Resharper 是一个非常强大的C#编程辅助工具,有着非常强的提示功能,代码纠正,代码简化等等 在编码过程中注意到这么一件事,可能是大家经常会遇到的: 遍历某个集合,然后经过处理生成另外一个集合,例如遍历一个产品列表,生成一个SelectItem 的List,我一直这么写: var list = new List<SelectListItem>(); foreach (var product in products) { list.Add(new SelectListItem(){ Tex…
描述:项目中使用了linq,发现写的顺序不一样最后的结果也不一样,效率也不一样. Linq的执行效率对比 List<int> source = new List<int>(); var rand = new Random(); ; ) { i--; source.Add(rand.Next(, )); } Stopwatch watch = new Stopwatch(); watch.Restart(); select s; int count2 = temp2.Count();…
* 使用foreach遍历数组时要注意的问题: * 1.foreach在遍历之前会自动重置指针使用其指向第一个元素,所以foreach可以多次遍历 * 2.foreach遍历完成之后,指针是没有指向数组的任何一个元素的,也就是此时数组没有指针指向,所以紧接着使用each遍历时没有元素输出 * 3.foreach与for循环的效率比较:foreach要优于for,因为for需要获得数组的长度通过i++才能遍历,而foreach不需要计算数组长度 * 4.foreach与while+each遍历的效…
在Linq中select子句用来指定查询结果的类型和表现形式.Linq查询要么以select子句结尾,要么以group子句结尾. List<UserBaseInfo> users = new List<UserBaseInfo>(); ; i < ; i++) { users.Add(new UserBaseInfo(i, "user0" + i.ToString(), "user0" + i.ToString() + "@w…
最近开始学习linq.自己也总结一下,方便以后查阅. Select 同 Sql 中的 select 类似,即输出我们要的东东,感觉在 linq 中更加强大. Linq 可以对集合如数组.泛型等操作,这里我们对泛型类型进行举例.建一个类如下: public class Customer { public Customer(string firstName, string lastName, string city) { FirstName = firstName; LastName = lastN…
先上代码 package com.test; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class Testttt { public static void main(String[] args) { // 实例化 List<String> stringLinkedList = new LinkedList<String>(); List<Str…
public static object SelectAnyInfo(){    DataAccessContext context = new DataAccessContext();    var query = from c in context.VideoNetServers                from cc in context.Areas                from ver in context.VersionItems                wher…
单个字段: var list1 = list.Select(field1 => field1.CouponID).ToList(); 多个字段: var list1 = list.Select(field1 => new { field1.CouponID, field1.EndDate }).ToList(); 全部映射: var list1 = list.Select(field1 => field1).ToList();…
class Program { static void Main(string[] args) { var listTest1 = new List<Test1> { "}, "}, "} }; var listTest2 = new List<Test2> { ",}, "}, "} }; var result = from t1 in listTest1 join t2 in listTest2 on t1.Key e…