Tag: list

我真的需要在集合上使用AsQueryable()吗?

示例代码: List Students = new List() { new Student(101, “Hugo”, “Garcia”, new List() { 91, 88, 76, 93 }), new Student(102, “Rick”, “Adams”, new List() { 70, 73, 66, 90 }), new Student(103, “Michael”, “Tucker”, new List() { 73, 80, 75, 88 }), new Student(104, “Fadi”, “Fakhouri”, new List() { 82, 75, 66, 84 }), […]

检查List 值是否连续

List dansConList = new List(); dansConList[0] = 1; dansConList[1] = 2; dansConList[2] = 3; List dansRandomList = new List(); dansRandomList[0] = 1; dansRandomList[1] = 2; dansRandomList[2] = 4; 我需要一个方法,在评估上面的列表时,将为dansRandomList返回false对于dansRandomList返回true ,基于事实dansConList在其值中有一个连续的数字序列,而dansRandomList则没有(缺少值3)。 如果可能,最好使用LINQ。 我试过的: 为了达到最终结果,我使用了for循环并与’i’(循环计数器)进行比较以评估值,但如上所述,我想使用LINQ。

检查空或空List

我有一个List,有时它是空的或null。 我希望能够检查它是否包含任何List项,如果没有,则将对象添加到List。 // I have a list, sometimes it doesn’t have any data added to it var myList = new List(); // Expression is always false if (myList == null) Console.WriteLine(“List is never null”); if (myList[0] == null) myList.Add(“new item”); //Errors encountered: Index was out of range. Must be non-negative and less than the size […]

为什么将List 转换为IList 导致性能降低?

我正在做一些性能指标,我遇到了一些对我来说很奇怪的事情。 我计时以下两个function: private static void DoOne() { List A = new List(); for (int i = 0; i < 200; i++) A.Add(i); int s=0; for (int j = 0; j < 100000; j++) { for (int c = 0; c < A.Count; c++) s += A[c]; } } private static void DoTwo() { List A […]

一次循环2个列表

我有两个长度相同的列表,是否可以一次循环这两个列表? 我正在寻找正确的语法来执行以下操作 foreach itemA, itemB in ListA, ListB { Console.WriteLine(itemA.ToString()+”,”+itemB.ToString()); } 你认为这在C#中有可能吗? 如果是的话,lambda表达式与此相当的是什么?

使用LINQ的唯一项目列表

我一直在使用LINQ一段时间了,但似乎被困在关于Unique项目的东西上,我有下面的列表: List stock = new List(); 它具有以下属性:字符串ID,字符串类型,字符串描述,示例: public class Stock { public string ID { get; set; } public string Type { get; set; } public string Description { get; set; } } 我希望有一个LINQ查询,它将按照类型对库存中的项目进行分组,并将其作为新的库存列表返回(它必须与原始库存列表的类型相同)。 示例数据: ID Type Description ———————————————- 1 Kitchen Appliance Dishwasher 2 Living Room Television 3 Kitchen Appliance Washing Machine 4 Kitchen Appliance […]

Array,ArrayList和List之间有什么区别?

我想知道Array , ArrayList和List之间的确切区别是什么(因为它们都有类似的概念)以及在哪里使用一个而不是另一个。 例: 排列 对于Array,我们只能添加我们为此示例声明的类型int 。 int[] Array = new Int[5]; //Instansiation of an array for(int i = 0; i < Array.Length; i++) { Array[i] = i + 5; //Add values to each array index } 数组列表 我们可以像数组一样添加值 ArrayList arrayList = new ArrayList(); arrayList.Add(6); arrayList.Add(8); 名单 我们可以像在数组中一样添加值 List list = new List(); list.Add(6); List.Add(8); […]

字典与字符串列表作为值

我有一个字典,我的值是List。 当我添加密钥时,如果密钥存在,我想在值(List)中添加另一个字符串? 如果密钥不存在,那么我创建一个带有值的新列表的新条目,如果密钥存在,那么我将jsut添加到List值ex的值。 Dictionary<string, List> myDic = new Dictionary<string, List>(); myDic.Add(newKey, add to existing list and not create new one)

Unity C#ArgumentOutOfRangeException:参数超出范围。 参数名称:index

我正在创造一个像蛇一样的游戏。 在我下面的代码中,蛇体的每个片段都是Character类的一个实例。 当我尝试添加新角色时,我收到错误: ArgumentOutOfRangeException: Argument is out of range. Parameter name: index 网上的其他资源已经提出有一个我想要引用的空列表。 但是在我的代码中我没有看到任何关于这种情况的证据。 也许比我更聪明的人可以找到我做错了什么? using System.Collections; using System.Collections.Generic; using UnityEngine; /* * This class controls the entire snake. The snake has a list * of segments that are each a character. We can move along the snake * by moving the characters bottom up, […]

Dictionary to List <KeyValuePair >其中KeyValuePair中的值为MyClass.ToString()

Dictionary dict = new Dictionary(); //where MyClass has an override for ToString() 现在如何从dict获取List<KeyValuePair> ,其中KeyValuePair中的值,如果MyClass.ToString()和KeyValuePair中的Key与dict的相同…? 有一个简单的方法吗? 我怎么能在那里使用IDictionary.ToList()函数? Plz启发..