linq 相关问题

语言集成查询(LINQ)是一种Microsoft .NET Framework组件,它为.NET语言添加了本机数据查询功能。请在适当时考虑使用更详细的标签,例如[linq-to-sql],[linq-to-entities] / [entity-framework]或[plinq]

LINQ 不包含“union”的定义

这个 linq 查询有什么问题,它向我显示了不包含“union”定义的错误 (来自 db.vM29s 中的修订版 其中 Years.Contains(rev.FinancialYear) && rev.Exclude==&

回答 2 投票 0

IASP.NET MVC 中的可查询操作

我是 IQueryable 的新手。我想用 OR 连接“selectedBus”中的所有项目,用 OR 连接“depositi”中的所有项目。 然后我想要 2 谓词(谓词和谓词 3)...

回答 1 投票 0

LINQ - 数据表分组不同计数

我正在尝试使用 linq 将 vb.Net 中的表连接在一起以返回主键的不同计数,但我似乎不太明白语法 这是创建表的代码 昏暗

回答 1 投票 0

如何使用 Linq(与 SelectMany 相反)展平嵌套实体?

我正在尝试将 SQL 转换为 Linq,将两个相关表扁平化为每个父表记录一条记录(与 SelectMany 相反。)我的第一次尝试如下,但生成的 SQL 是不同的

回答 1 投票 0

如何使用 linq 从另一个列表 <> 更新具有两个条件的列表 <>?

如何使用 linq 更新以下方法来更新循环内的循环,并具有两个条件? 我看过其他类似的帖子,但没有两个条件。 foreach(变量...

回答 2 投票 0

LINQ 中 AsEnumerable() 的内部实现

我有两个问题: 问题 1 背景: 在查看 Microsoft LINQ 中“AsEnumerable()”方法的实现时,我注意到: 公共静态 IEnumerable

回答 1 投票 0

从对象列表中获取不同属性值的列表

在 C# 中,假设我有一个名为 Note 的类,其中包含三个字符串成员变量。 公开课注意事项 { 公共字符串标题; 公共字符串作者; 公共字符串文本; } 我有一个类型列表...

回答 7 投票 0

使用具有动态 linq 和 order by 子句的自动映射器

我正在使用自动映射器进行动态查询,并且在我使用 orderby 子句之前它运行良好。 这就是我创建查询的方式: IQueryable查询; //配置l'

回答 1 投票 0

如何使用 LINQ 根据不同属性的值修改功能标记属性

我有以下 XML 源: 我有以下 XML 来源: <?xml version="1.0"?> <report> <feature tag="Config"/> <feature tag="Runtime"> <feature tag="Metadata"> <property name="date" value="16.01.2025"/> <property name="time" value="09:31:34"/> </feature> <feature tag="Templates"> <feature tag="Template"> <property name="username" value="myself"/> <property name="password" value="something"/> <feature tag="Data sources"> <feature tag="Source"> <property name="name" value="modules"/> <property name="driver" value="eval"/> </feature> <feature tag="Source"> <property name="name" value="Artifact"/> <property name="driver" value="eval"/> </feature> <feature tag="Source"> <property name="name" value="Comments"/> <property name="driver" value="eval"/> </feature> </feature> </feature> </feature> </feature> </feature> </report> 我想修改 driver 属性的 value(位于功能标签 Source 下方),但前提是 name 属性的值(位于功能标签 Source 下方)等于单词“模块”。 我尝试使用以下函数仅提取特征标签Source。我认为可以在一个 LINQ 命令中按照我想要的方式修改属性,但我不知道如何在 XPath 中制定此 if 构造。 Private Function ModifyXml(ByVal xml As String) As Boolean Try Dim xdoc As New XDocument xdoc = XDocument.Parse(xml) Dim query As String = "/report/feature[@tag='Runtime']/feature[@tag='Templates']/feature[@tag='Template']/feature[@tag='Data sources']/feature[@tag='Source']" xdoc.XPathSelectElements(query).ToList() xdoc.Save("c:\temp\myFile.xml") Return True Catch ex As Exception Return False End Try End Function 结果应该是这样的: 我需要属性中的驱动程序名称作为值。该驱动程序名称取决于名称属性的值。 <?xml version="1.0"?> <report> <feature tag="Config"/> <feature tag="Runtime"> <feature tag="Metadata"> <property name="date" value="16.01.2025"/> <property name="time" value="09:31:34"/> </feature> <feature tag="Templates"> <feature tag="Template"> <property name="username" value="myself"/> <property name="password" value="something"/> <feature tag="Data sources"> <feature tag="Source"> <property name="name" value="modules"/> <property name="driver" value="somedriver1"/> </feature> <feature tag="Source"> <property name="name" value="Artifact"/> <property name="driver" value="somedriver2"/> </feature> <feature tag="Source"> <property name="name" value="Comments"/> <property name="driver" value="somedriver3"/> </feature> </feature> </feature> </feature> </feature> </feature> </report> XPath 查询,它不会修改。顾名思义, XPathSelectElements 返回所需的节点。你必须处理 其中每一个并修改正确的子元素。这 非常规的 XML 模式使这变得比需要的更加困难 -模板、来源、评论等都应该是元素,而不是通用属性中的值。当前格式无法获得灵活性。 LINQ也是一种查询,而不是一种修改语言,它不会修改。 无论您使用哪种语言,都必须迭代 他们产生的特征元素,并为每个元素检索名称或 驱动孩子并修改他们的值属性。 Panagiotis Kanavos 的评论

回答 1 投票 0

将不同的值添加到并发字典内的列表中

我想将小数添加到 ConcurrentDictionary 内的列表中 ConcurrentDictionary> fullList = 新的并发词典 我想将小数添加到 ConcurrentDictionary 内的列表中 ConcurrentDictionary<string, List<decimal>> fullList = new ConcurrentDictionary<string, List<decimal>>(); public void AddData(string key, decimal value) { if (fullList.ContainsKey(key)) { var partialList = fullList[key]; partialList.Add(value); } else { fullList.GetOrAdd(key, new List<decimal>() { value }); } } 从技术上讲,上面的代码对我有用,但只是这样做,因为我不知道如何执行 GetOrAdd 方法来添加和更新。我的问题是,考虑到我的更新将在现有列表的末尾添加一个项目,我该如何使用该方法? 您可以使用 AddOrUpdate 方法来达到此目的,如下所示: fullList.AddOrUpdate(key, new List<decimal>() { value }, (key,list) => { list.Add(value); return list; }); 本质上,当 key missing 时,会创建一个仅包含一个元素 value 的新列表。否则,我们将 key 添加到与当前 value 关联的列表中,然后返回该列表。 有关此方法的文档,请查看这里。 您在寻找这样的东西吗?希望有帮助。 ConcurrentDictionary<string, List<decimal>> fullList = new ConcurrentDictionary<string, List<decimal>>(); public void AddData(string key, decimal value) { List<decimal> list; if (!fullList.TryGetValue(key, out list)) { list = new List<decimal>(); fullList.GetOrAdd(key, list); } list.Add(value); } 您只需要在所有情况下调用GetOrAdd: public void AddData(string key, decimal value) { var partialList = fullList.GetOrAdd(key, _ => new List<decimal>()); lock (partialList) partialList.Add(value); // Lock for thread safety } 当您使用 ConcurrentDictionary 时,您应该努力使用其特殊的并发 API,以强制执行操作的原子性。分两步检查和添加 (ContainsKey+Add) 不是原子的,它会引入竞争条件,并可能损害应用程序的正确性。

回答 3 投票 0

使用 LINQ 根据特定属性值删除特定 XML 元素

作为源,我有一个包含以下内容的 XML 文档: 作为源,我有一个包含以下内容的 XML 文档: <?xml version="1.0"?> <report> <feature tag="Config"/> <feature tag="Runtime"> <feature tag="Output"> <feature tag="Target"> <property name="type" value="Html"/> <property name="driver" value="Telelogic.Html.Driver"/> </feature> <feature tag="Target"> <property name="type" value="Word"/> <property name="driver" value="Telelogic.Word.Driver"/> </feature> <feature tag="Target"> <property name="type" value="PDF"/> <property name="driver" value="Telelogic.Pdf.Driver"/> </feature> </feature> </feature> </report> 如果 Target 元素中的属性值不是 Word,我想删除整个元素,如下所示: <?xml version="1.0"?> <report> <feature tag="Config"/> <feature tag="Runtime"> <feature tag="Output"> <feature tag="Target"> <property name="type" value="Word"/> <property name="driver" value="Telelogic.Word.Driver"/> </feature> </feature> </feature> </report> 因此,我准备了以下vb.net函数: Public Function removeElements(ByVal source As String) As Boolean Try Dim doc As New XDocument Dim output As String = "/report/feature[@tag='Runtime']/feature[@tag='Output']" Dim propertyType As String = "/report/feature[@tag='Runtime']/feature[@tag='Output']/feature[@tag='Target']/property[@name='type']" Dim path As String = "c:\temp\mySource.xml" doc = XDocument.Parse(source) doc.XPathSelectElements(output).Elements _ .Where(Function(x) x.XPathSelectElement(propertyType).Attribute("value").Value <> "Word").Remove doc.Save(path) Return True Catch ex As Exception Return False End Try End Function 问题是,我的代码删除了所有 Target 元素,因此 Output 标记为空,正如您在结果中看到的: <?xml version="1.0" encoding="utf-8"?> <report> <feature tag="Config"></feature> <feature tag="Runtime"> <feature tag="Output" /> </feature> </report> 在 LINQ 的帮助下,我找不到任何解决方案来删除每个 Target 元素,除了那个将字符串“Word”作为属性值的元素之外。 这是一个用 c# 实现的工作解决方案。 您可以将其转换为VB.NET。 c# void Main() { const string inputXML = @"e:\Temp\input.xml"; const string outputXML = @"e:\Temp\output.xml"; XDocument xdoc = XDocument.Load(inputXML); xdoc.XPathSelectElements("/report/feature[@tag='Runtime']/feature[@tag='Output']//feature[not(property/@value='Word')]") .ToList() .ForEach(x => x.Remove()); xdoc.Save(outputXML); }

回答 1 投票 0

获取List中不同值的列表

在 C# 中,假设我有一个名为 Note 的类,其中包含三个字符串成员变量。 公开课注意事项 { 公共字符串标题; 公共字符串作者; 公共字符串文本; } 我有一个类型列表...

回答 7 投票 0

服务器响应为:5.7.0 需要身份验证

我尝试运行此代码,它显示此错误。这是重置密码,用户需要输入电子邮件地址。当电子邮件地址有效时,管理员会将链接发送给用户...

回答 3 投票 0

根据派生或子对象列表的条件选择父对象列表

我有一个 UserTaskGroup 的集合作为 List,其中每个 UserTaskGroup 对象都有一个 List,意味着对于 1 个任务组,可能有一个或多个任务。 UserTask 类有一个名为

回答 1 投票 0

c# linq xml 如何对同一名称空间使用两个前缀

Linq 中有没有办法对同一名称空间使用两个前缀?例如,“foo1”和“foo2”下面有两个前缀,指向我想在文档中使用的同一名称空间。我该怎么办...

回答 2 投票 0

在 LINQ 中执行查询花费的时间太长

我编写了一个查询,该查询应该从一个表中获取所有行,并对第二个表执行子查询,仅从最新记录中提取一个值。 在 SQL Server 中,查询大约需要 15 秒...

回答 3 投票 0

计算属性和实体框架

我在查询 linq 时遇到问题。 我有这样的课程: 公共抽象类主题:IValidatableObject { 公共抽象字符串全名{ get; } 公共抽象主题类型主题类型...

回答 1 投票 0

在 EF 6 中重新加载导航属性

我已经阅读了很多有关使用重新加载导航属性的 stackoverflow 问题 Context.Entry(实体).Collection(p => p.Property).Load() 但就我而言,它没有给我更新...

回答 1 投票 0

如何通过嵌套集合 max 属性进行排序

我有一张Users表,每个表可能有很多LogActivity,所以存在一对多关系。我想按 LogActivities 的最大属性 DateSearch 对用户集合进行排序(在其他情况下...

回答 1 投票 0

如何使用 Linq 比较两个复杂列表

我从未使用过 linq,但现在我必须使用,而且我在使用它时遇到了一些麻烦。下面是我通常如何比较两个列表并检索不良和良好价格记录的示例。我希望...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.