Tag: linq

如何合并两个不同对象的列表?

使用C#和LINQ,如何合并两个不同对象的列表,比如研讨会和会议? 它们有一些共同的和一些不同的字段/属性,并且不共享唯一的id。 class Seminar { int id, DateTime joinDate, string name } class Conference { Guid confNumber, DateTime joinDate Type type } 我有一个列表: List List 我需要将它们合并到一个超级List : List 代码片段会很有帮助。

如何使用LINQ,C#向元素添加属性?

我有一个XElement对象,它有许多属性,我只想在元素中添加另一个属性。 我该怎么做呢?

LINQ加入多表

我有4张桌子: table1 id1, fk_tbl2 //this is the foreign key to the “id” in table2 table2 id2, fk_tbl3 //this is the foreign key to the “id” in table3 table3 id3, fk_tbl4 //this is the foreign key to the “id” in table4 table4 id4, name 我想创建一个多表连接,当用户输入“id4”时,我可以获得“table1”中的记录列表。 如何在C#中编写连接? 谢谢。

linq查询表

如何将以下代码转换为linq或谓词表达式: List list1 = new List {1,2,3,4,5}; List list2= new List {2,3,4,5,6,7}; foreach (int int1 in list1) list2.Remove(int1);

C#如何将IEnumerable匿名列表转换为数据表

有许多解决方案可以将列表转换为使用reflection的DataTable ,这可用于转换匿名类型。 但是,它有很多匿名类型的列表,然后性能可能是一个问题。 这是从列表创建DataTable的唯一方法吗? 有更快的方法吗?

LINQ中的动态where子句?

我正在尝试根据Dynamic where条件加载数据。 string tempQry = string.Empty; if (!string.IsNullOrEmpty(cusid) && !string.IsNullOrEmpty(mktid)) tempQry = “x=>x.MarketID==” + mktid + “&& x.MasterCustomerID==” + cusid; if (string.IsNullOrEmpty(cusid)) tempQry = “x=>x.MarketID==” + mktid; if (string.IsNullOrEmpty(mktid)) tempQry = “x=>x.MasterCustomerID==” + cusid; _lstOptInInterest = new LinkedList( (from a in _lstOptInInterest join b in _marketoEntities.CustCommPreferences.Where(tempQry) on new { CODE = a.Code, SUBCODE = a.SubCode […]

Null在调用链中合并

如果我有一长串对象,每个对象都有可能在“Linq where”子句中返回null,例如 SomeSource.Where(srcItem=>(srcItem.DataMembers[“SomeText”].Connection.ConnectedTo as Type1).Handler.ForceInvocation == true)); 索引器可以返回null,“as”运算符可以返回null。 对象可能没有连接(即属性为null)。 如果在任何地方遇到null,我希望where子句为被评估的项返回“false”。 相反,它以null引用exception中止。 在我看来,这将被设计为在单个C#表达式中表达。 我不喜欢创建一个多行语句或为它创建一个单独的函数。 我是否缺少使用空合并运算符?

在C#中合并包含List的字典

这与这个问题有关 ,关于如何在C#中合并两个词典。 提出了一个优雅的Linq解决方案,很酷。 但是,该问题与Dictionary,而我有一个字典,其值为List. 我正在寻找一个合并Dictionary<Object1, List>,的解决方案Dictionary<Object1, List>,具有以下要求: 如果Dictionary1包含与Dictionary2相同的键,则应组合它们的List列表。 您最终会得到一个带有共享密钥的新键值对,以及来自两个词典的组合列表。 如果Dictionary1包含Dictionary2没有的键,则Dictionary1中的List列表应该成为值,反之亦然。 这在Linq中可能是不可能的,或者可能值得用for循环等来写出来,但是有一个优雅的解决方案会很好。

将具有多个froms的linq查询表达式转换为扩展方法语法

我将此代码转换为扩展方法语法时遇到问题: var query = from c in _context.Customers from o in c.Orders where o.DateSent == null select new CustomerSummary { Id = c.Id, Username = c.Username, OutstandingOrderCount = c.Orders.Count }; 有任何想法吗?

如何将项目列表从视图传递到控制器(ASP.NET MVC 4)

我正在尝试将List 类型的多个项目传递给我的控制器,但是,当我提交数据时,它在我的控制器中显示为null。 我想要发生的是,我在视图中有一个“费用”列表,并且每个“费用”或项目旁边都是我的模型中的“已提交的布尔”属性的复选框。 当我检查项目时,我希望在数据库中更新已检查项目的属性列表Submitted和DateSubmitted 。 View中的@ Html.DisplayFor(modelItem => item.Submitted)生成复选框。 我究竟做错了什么? 这是我的观点: @model IEnumerable @{ ViewBag.Title = “Submit Expenses”; Layout = “~/Views/Shared/_Layout.cshtml”; DateTime today = DateTime.Today; string formattedDate = today.ToString(“MM/dd/yyyy”); } Submit Expenses @using (Html.BeginForm()) { @Html.AntiForgeryToken() Start Date: @Html.TextBox(“expenseDate”, formattedDate, htmlAttributes: new { @class = “form-control” }) End Date: @Html.TextBox(“expenseDate2”, formattedDate, htmlAttributes: new { @class […]