Tag: linq

使用lambda表达式从List 获取N max个数字

这是我的清单: List numbers=new List { 12, 5, -8, 4, 7, 28, 3, 22 }; 如何通过lambda获得4个最大数字:我需要这些数字: {28, 22, 12, 7}

LINQ – 从嵌套列表中获取值的总数

我有一个Class : public class Company { public int id { get; set; } public string title { get; set; } public string address { get; set; } public string city { get; set; } public string zip { get; set; } public List phones { get; set; } public List categories { get; set; } […]

在mongodb集合上无法做到这一点

我有两个模型,一个用户和一个团队,如下所示: [BsonRepresentation(BsonType.ObjectId)] public ObjectId _id { get; set; } [Display(Name = “Password:”)] public string Password { get; set; } [Display(Name = “Confirm:”)] public string ConfirmPassword { get; set; } [Display(Name = “Email:”)] public string Email { get; set; } [Display(Name = “Username:”)] public string UserName { get; set; } [Display(Name = “Firtname:”)] public string Firstname […]

不能将Enumerable.Count与List一起使用,编译器假定为List.Count

我还没有注意到这种行为,可能是因为我更喜欢VB.NET中的查询语法,并将查询和执行方法分成不同的语句。 如果我尝试编译以下简单查询: Dim wordList As List(Of String) = New List(Of String) Dim longWords As Int32 = wordList.Count(Function(word) word.Length > 100) 编译器不喜欢这样,因为他希望List.Count没有参数: “Public Readonly Property Count As Integer”没有参数,其返回类型无法编入索引。 如果我将它声明为IEnumerable(Of String)它按预期工作: Dim wordSeq As IEnumerable(Of String) = New List(Of String) Dim longWords As Int32 = wordSeq.Count(Function(word) word.Length > 100) 为什么会这样? 什么阻止编译器使用Enumerable扩展方法Count而不是ICollection.Count属性。 请注意,我添加了Imports System.Linq , Option Strict和Option Infer都On […]

使用LINQ如何在集合中连接itesm的字符串属性

我有一个集合中的对象列表。 每个对象都有一个名为Issue的字符串属性。 我想从集合中的所有项目连接问题并将它们放入单个字符串中。 什么是使用LINQ做到这一点最干净的方法。 这是手动方式: string issueList = “”; foreach (var item in collection) { if (!String.IsNullOrEmpty(item.Issue) { issueList = issueList + item.Issue + “, “; } } //Remove the last comma issueList = issueList.Remove(issueList.Length – 2); return issueList;

Linq中的Regex(EntityFramework),数据库中的字符串处理

我的表中有一列包含值的列 “FilterA:123,234,34;FilterB:12,23;FilterC:;FilterD:45;” filter由’;’分隔 并且每个filter的值由’,’分隔。 Filter的名称和值之间有一个’:’。 现在,我可以做任何只能取出值部分的东西吗? 像“FilterA”的“123,234,34”。 或者我可以给它一个像“234”这样的数字来搜索“FilterA”的值部分和/或“FilterB”的值部分中的“54”吗? 我知道使用正则表达式是可能的,我猜,但我不知道怎么做。

使用LINQ从XML文件中选择元素

我有这个XML结构: My Work Main Building 1 relativeToGround Office 1 1 relativeToGround 这继续…… 我需要为每个建筑物选择建筑物“名称”并将其存储在列表中。 我写了这段代码: using System; using System.Linq; using System.Xml; using System.Xml.Linq; using System.Collections.Generic; namespace dsdsdsds { public class Building { public string BuildingName { get; set; } } class MainClass { public static void Main(string[] args) { List buildingNames = (from e in XDocument.Load(“buildings.kml”).Root […]

在GroupBy中连接字符串

我在GroupBy上用IQueryable做了一个GroupBy 。 但是我想在串联字符串上做一个GroupBy 。 我现在拥有的是: SomeList.GroupBy(x => x.Code + x.Location) 这适用于我,但是这也将x.Code更改为连接字符串x.Code + x.Location 。 有没有办法临时连接GroupBy变量,如: SomeList.GroupBy(x => (x.Code + x.Location) as TempVar)

如何使用LINQ解析可选或空的XML元素和属性?

我有以下类型的XML: 通过这个XML,我希望填充以下objectList。 List objFunctionsList = new List(); 其中Function类如下, public class Function { public String Name { get ; set; } public Parameter ReturnType { get; set; } public List Parameters { get; set; } public String Library { get; set; } public String Signature { get; set; } public String Description { get; set; } […]

带有空格的变量名称

我想要一个带有空格的变量名。 我将此集合传递给gridview,我需要标题有一个空格而不是下划线 var registrationReport = (from r in BankMedEntity.Customers select new {r.Id,Customer Type = r.CustomerType_Id }); 关于如何实现这一点的任何想法? 我需要将标题设为“客户类型”而不是“CustomerType_Id”