Tag: list

C#:List 中的更新项值

我正在寻找一种方法来更新列表中的元素,而不是自己枚举它。 我得到了包含名为Projects的List的Class MyProjects。 我想找到MyProjects.Projects-Class,其中Class1(Name)的成员属性等于Value“Overhead”。 什么有效: foreach (Project prj in MyProjects.Projects) { if (prj.Name == “Overhead”) prj.IsActive = true; }; 但是,我尝试使用Linq来做同样的事情,但是把它写成一行却失败了。 这有可能吗? 我不喜欢以上述方式进行迭代的原因是我已经迭代了这个代码块中的整个列表并认为可能有更美妙的方式:)

C#序列化并反序列化json到txt文件

我正在使用NewtonSoft在我的wpf应用程序中处理json。 我有一个客户可以保存到txt文件(不涉及数据库)。 我是这样做的: public int store(string[] reservation) { JObject customer = new JObject( new JProperty(“id”, this.getNewId()), new JProperty(“name”, reservation[0]), new JProperty(“address”, reservation[1]), new JProperty(“gender”, reservation[2]), new JProperty(“age”, reservation[3]) ); using (StreamWriter file = File.CreateText(Settings.databasePath + “customer.json”)) using (JsonTextWriter writer = new JsonTextWriter(file)) { customer.WriteTo(writer); } return 1; } 结果如下: {“id”:1,”name”:”Lars”,”address”:”Bosch 10″,”gender”:”Man”,”age”:”19″} 然后我试图让所有客户都这样: if(File.Exists(Settings.databasePath + “customer.json”)) […]

linq列出并获取每行的值

对于测试,我需要使用一些参数设置制作列表。 此列表不是根据定义预先定义为类型和/或其中的内容。 bool[] trueOrFalse = new bool[] { true, false }; int[] para1 = new int[] { 1, 2, 3 }; int[] para2 = new int[] { 5, 6, 7 }; int[] para3 = new int[] { 1, 2, 3 }; int[] para4 = new int[] { 5, 7, 9 }; List test = (from […]

“项目不存在”错误读取SharePoint 2010列表

我在SharePoint 2010中有一个列表。如果我以编程方式(通过自定义webpart)将项目添加到列表中,我可以稍后阅读这些项目并在其他Web部件中显示它们。 但是,如果我尝试读取通过Web界面添加的列表项,我的webpart中会出现以下错误: 物品不存在。 您选择的页面包含不存在的项目。 它可能已被其他用户删除.x81020016 奇怪的是,在调试器中,我看到列表项已正确读取。 我把头发上留下的东西拉过来。 有任何想法吗? 以下是任何关心的人的答案: 我这样调用我的页面 – mywebpage.aspx?id = 1,其中id = 1是我希望我的webpart显示的列表中项目的ID。 由于某些只有Microsoft知道的原因,在查询字符串中使用’id’是禁止的。 所以我将param名称更改为’lid’,现在一切都像我期望的那样工作。 感谢大家的回复。

计算List 中相似的相邻项

我正在尝试在List中找到类似的相邻项目并计算其编号,例如: List list = new List {“a”, “a”, “b”, “d”, “c”, “c”}; 期望的输出: a = 2,c = 2 我所做的是使用for循环遍历列表的每个元素并查看它是否具有相似的相邻元素,但可以理解的是它给出了ArgumentOutOfRangeException()因为我不知道如何跟踪迭代器的位置所以它不会超出范围。 这就是我所做的: for (int j = 0; j < list.Count; j++) { if (list[j] == "b") { if ((list[j + 1] == "b") && (list[j – 1] == "b")) { adjacent_found = true; } } } 话虽如此,如果除了使用for循环迭代之外,还有另一种更简单的方法可以在List中找到相似的相邻元素,请提供建议。 […]

C# – 查找两个List s – Lambda语法的公共成员

所以我写了这个简单的控制台应用程序,以帮助我的问题。 在方法的第3行使用lambda表达式来获取公共成员的正确方法是什么。 尝试了Join()但无法弄清楚正确的语法。 作为后续行动……有没有一种非LINQ方式在我错过的一行中做到这一点? class Program { static void Main(string[] args) { List c = new List() { 1, 2, 3 }; List a = new List() { 5, 3, 2, 4 }; IEnumerable j = c.Union(a); // just show me the Count Console.Write(j.ToList().Count.ToString()); } }

通用列表,使用条件语句计数的项目

我有一个通用列表。 它有一个ListfilesToProcess.Count属性,它返回项目的总数,但我想用条件语句计算列表中的某些项目数。 我是这样做的: int c = 0; foreach (FilesToProcessDataModels item in ListfilesToProcess) { if (item.IsChecked == true) c++; } 是否有任何更短的方式,如int c = ListfilesToProcess.count(item => item.IsChecked == true);

C#:实例化一个List,给出一个Type 的引用

假设在C#中, myType是对某些Type的引用。 仅使用myType ,是否可以创建myType对象List ? 例如,在下面的代码中,虽然它是错误的,但我想实例化via new List < myType > ( ) 。 using System ; using System.Reflection ; using System.Collections.Generic ; class MyClass { } class MainClass { public static void Main ( string [] args ) { Type myType = typeof ( MyClass ) ; List myList = new List ( ) […]

如何将数据源绑定到List <Dictionary >?

我有一个存储字典条目列表的类。 我想从代码隐藏绑定到gridview的数据源。 字典类型的代码,表示ErrorMessage和失败字段。 public partial class FailedFields { private Dictionary Code_Error = new Dictionary(); public void AddFailedField(string field, string message) { Code_Error.Add(field, message); } public Dictionary GetFailedFields() { return Code_Error; } } 字典条目列表的代码。 public partial class ErrorFieldsList { private static List ErrorList = new List(); public void AddErrorField(Order.FailedFields errs) { ErrorList.Add(errs); } public List GetErrorMessages() […]

Sqlite一对多关系

我正在制作一个需要存储和从sqlite数据库获取数据的应用程序。 我目前使用的两个Nuget包是Sqlite PCL和SQLite-Net Extensions 。 [Table(“patient”)] public class Patient { [PrimaryKey] public string ID { get; set;} public string FirstName { get; set; } public string LastName { get; set; } [OneToMany] public List PatientVitals { get; set; } } [Table(“patientVitals”)] public class PatientVitals { [PrimaryKey] public string VitalID {get;set;} public string Weight { get; […]