草庐IT

LINQ_ENABLED

全部标签

c# - LINQ 一次更新多个元素

是否可以使用LINQ为List中的每个元素设置属性。例如:varusers=fromuincontext.Userswhereu.Name=="George"selectu;foreach(Userusinusers){us.MyProp=false;}有没有可能让它更干净? 最佳答案 或者您可以将其转换为ToList()并使用ForEach方法。users.ToList().ForEach(u=>u.MyProp=false);更新多个属性users.ToList().ForEach(u=>{u.property1=value1;

c# - LINQ 的延迟执行,但是如何呢?

这一定很简单。但我还是要问,因为我认为其他人也会为此而苦苦挣扎。为什么以下简单的LINQ查询并不总是使用新的变量值而不是总是使用第一个来执行?staticvoidMain(string[]args){Console.WriteLine("Entersomething:");stringinput=Console.ReadLine();//forexampleABC123vardigits=input.Where(Char.IsDigit);//123while(digits.Any()){Console.WriteLine("Enterastringwhichdoesn'tcontai

c# - LINQ 的延迟执行,但是如何呢?

这一定很简单。但我还是要问,因为我认为其他人也会为此而苦苦挣扎。为什么以下简单的LINQ查询并不总是使用新的变量值而不是总是使用第一个来执行?staticvoidMain(string[]args){Console.WriteLine("Entersomething:");stringinput=Console.ReadLine();//forexampleABC123vardigits=input.Where(Char.IsDigit);//123while(digits.Any()){Console.WriteLine("Enterastringwhichdoesn'tcontai

c# - LINQ 中的条件 "orderby"排序顺序

在LINQ中,是否可以按排序顺序(升序与降序)进行条件排序。像这样(无效代码):boolflag;(fromwinwidgetswherew.Name.Contains("xyz")orderbyw.Id(flag?ascending:descending)selectw) 最佳答案 如果您逐步构建表达式,则可以做到这一点。通常使用表达式比理解表达式更容易:varx=widgets.Where(w=>w.Name.Contains("xyz"));if(flag){x=x.OrderBy(w=>w.property);}else{x

c# - LINQ 中的条件 "orderby"排序顺序

在LINQ中,是否可以按排序顺序(升序与降序)进行条件排序。像这样(无效代码):boolflag;(fromwinwidgetswherew.Name.Contains("xyz")orderbyw.Id(flag?ascending:descending)selectw) 最佳答案 如果您逐步构建表达式,则可以做到这一点。通常使用表达式比理解表达式更容易:varx=widgets.Where(w=>w.Name.Contains("xyz"));if(flag){x=x.OrderBy(w=>w.property);}else{x

c# - 使用 LINQ 对对象列表进行分组

我有一个对象:publicclassCustomer{publicintID{get;set;}publicstringName{get;set;}publicintGroupID{get;set;}}我返回一个可能如下所示的列表:ListCustomerList=newList();CustomerList.Add(newCustomer{ID=1,Name="One",GroupID=1});CustomerList.Add(newCustomer{ID=2,Name="Two",GroupID=1});CustomerList.Add(newCustomer{ID=3,Name=

c# - 使用 LINQ 对对象列表进行分组

我有一个对象:publicclassCustomer{publicintID{get;set;}publicstringName{get;set;}publicintGroupID{get;set;}}我返回一个可能如下所示的列表:ListCustomerList=newList();CustomerList.Add(newCustomer{ID=1,Name="One",GroupID=1});CustomerList.Add(newCustomer{ID=2,Name="Two",GroupID=1});CustomerList.Add(newCustomer{ID=3,Name=

c# - LINQ ForEach 可以有 if 语句吗?

是否可以在LINQForEach调用中添加if语句?sequence.Where(x=>x.Name.ToString().Equals("Apple")).ToList().ForEach(/*Ifstatementhere*/); 最佳答案 您可以执行以下操作...List.Where(x=>x.Name.ToString().Equals("Apple")).ToList().ForEach(x=>{if(x.Name==""){}}); 关于c#-LINQForEach可以有if

c# - LINQ ForEach 可以有 if 语句吗?

是否可以在LINQForEach调用中添加if语句?sequence.Where(x=>x.Name.ToString().Equals("Apple")).ToList().ForEach(/*Ifstatementhere*/); 最佳答案 您可以执行以下操作...List.Where(x=>x.Name.ToString().Equals("Apple")).ToList().ForEach(x=>{if(x.Name==""){}}); 关于c#-LINQForEach可以有if

c# - 在 LINQ 中表达递归

我正在为分层数据源编写一个LINQ提供程序。我发现通过编写示例展示我想如何使用它,然后编写代码来支持这些用例来设计我的API是最简单的。我遇到的一件事是在LINQ语句中以一种简单/可重用/优雅的方式表达“深度查询”或递归。换句话说,什么是最好的区分方式:fromiteminimmediate-descendants-of-current-nodewhere...selectitem对比:fromiteminall-descendants-of-current-nodewhere...selectitem(编辑:请注意,以上示例都不一定反射(reflect)我想要的查询结构。我对任何表达