随笔分类 - LINQ
摘要:Cast<T>() 方法是 LINQ 中的一个标准查询运算符,用于将非泛型集合转换为泛型集合。 使用场景1)类型转换:当你需要将一个非泛型集合(如 IEnumerable)转换成一个泛型集合(如 IEnumerable<T>)时,可以使用 Cast<T>() 方法。这在处理旧的API或遗留代码时尤其
阅读全文
摘要:示例 1:多层集合展平 假设你有一个列表,每个元素都是一个字符串数组,你想将所有的字符串展平成一个单一的字符串列表。 示例 2:嵌套循环 假设你有一个用户列表,每个用户有一个订单列表,你想获取所有用户的订单列表。 示例 3:多对多关系 假设你有一个学生列表,每个学生选修了多门课程,你想获取所有学生选
阅读全文
摘要:如何根据一列或多列来去重? 答案见下面: https://stackoverflow.com/questions/14321013/distinct-in-linq-based-on-only-one-field-of-the-table
阅读全文
摘要:见stackflow上的答案: https://stackoverflow.com/questions/57406327/what-is-the-difference-between-group-n-by-vs-group-n-by-into-g-in-linq
阅读全文
摘要:1.数据准备: 假设有几个重复数据,如下,(正常使用Distinct()方法,我们想要排除掉重复的对象) using System.Collections.Generic; namespace LINQTutorial { public class Student { public int ID {
阅读全文
摘要:这边以Sum为例,Max,Min,Average用法类似 一、 Array static void SumArray() { Console.WriteLine("SumArray:"); int[] intNumbers = new int[] { 10, 30, 50, 40, 60, 20,
阅读全文
摘要:一、数据准备 public class Student { public int ID { get; set; } public string Name { get; set; } public string Gender { get; set; } public string Branch { g
阅读全文
摘要:一、数据准备 public class Employee { public int ID { get; set; } public string Name { get; set; } public int AddressId { get; set; } public int DepartmentId
阅读全文
摘要:一、 数据准备 public class Employee { public int ID { get; set; } public string Name { get; set; } public int AddressId { get; set; } public static List<Emp
阅读全文
摘要:1. 数据准备 Employee类如下:(构造数据时,特意把奇数ID的Employee设置成没有Department) public class Employee { public int ID { get; set; } public string Name { get; set; } publi
阅读全文
摘要:1. 数据准备 using System.Collections.Generic; namespace LINQTutorial { public class Employee { public int ID { get; set; } public string Name { get; set;
阅读全文
摘要:1.IEnumberable IEnumerable<Customers> customers =Customers.Where(q => q.CompanyName.StartsWith("A")); customers = customers.Take<Customers>(10); custo
阅读全文
摘要:例子参照网址: https://blog.csdn.net/weixin_34185512/article/details/94654128 测试结果是:linq的执行效率基本上都要比用基础代码编写的foreach要慢一点。(跟上一篇一样的结论) 1.测试一代码如下: using System.Co
阅读全文
摘要:文章标题:Is Using LINQ in C# Bad for Performance? 时间:2020年7月13日 文章网址:https://medium.com/swlh/is-using-linq-in-c-bad-for-performance-318a1e71a732 个人从这篇文章学到
阅读全文
摘要:1.Insert context.User.InsertOnSubmit(u); context.SubmitChanges(); 2.Update context.SubmitChanges(); 3.Delete context.User.DeleteOnSubmit(u); context.S
阅读全文