Tag: linq

Linq to Entities无法识别string.Format或concatenation’+’

我有下面的代码: using (DBContext context = new DBContext()) { myCollection = context.Items.Where(i => i.Type == 1).OrderBy(k => k.Name).Select(w => new { Alias = w.Name + string.Format(“{0}”, w.Id), Name = w.Name }).ToArray(); } 在运行时,我尝试连接字符串并尝试con转换整数w.Id为字符串时出错。 错误说: Linq to实体无法识别方法string.Format 也不支持串联符号’+’。 我通过引入AsEnumerable解决了这个问题: using (DBContext context = new DBContext()) { myCollection = context.Items.AsEnumerable().Where(i => i.Type == 1).OrderBy(k => k.Name).Select(w => new […]

在C#中检查两个List 列表是否相等的最佳方法是什么?

有很多方法可以做到这一点,但我觉得我错过了一个function或其他东西。 显然List == List将使用Object.Equals()并返回false 。 如果列表中的每个元素都相等并且出现在相反列表中的相同位置,那么我认为它们是相等的。 我正在使用值类型,但正确实现的Data对象应该以相同的方式工作(即我不是在寻找浅复制列表,只是内部每个对象的值相同)。 我尝试过搜索,也有类似的问题,但我的问题是每个元素都是按照确切的顺序相等。

Linq Sub-Select

如何在LINQ中编写子选择。 如果我有一个客户列表和一个订单列表,我希望所有没有订单的客户。 这是我的伪代码尝试: var res = from c in customers where c.CustomerID ! in (from o in orders select o.CustomerID) select c

怎么才能得到linq的计数

我有一个名为带列的products的表: productid , productname, productprice categoryid 我的问题是我想根据产品名称和细节获得产品数量。 我想在DataGridView显示数据。 我如何知道单个产品名称的产品数量,如下所示? productid productname productavailable productprice ————————————————————————– 1 product A 2 products(product A) 100 2 product B 5 Products(product B) 200 像上面的表格一样,我必须在DataGridView显示。 我正在使用LINQ和C#,我的DbContext名称是tsgdbcontext 。

ASP:ListBox获取所选项目 – 一个class轮?

我试图获取asp:ListBox控件的选定项目,并将它们放在逗号分隔的字符串中。 那么必须有一种更简单的方法: foreach (ListItem listItem in lbAppGroup.Items) { if (listItem.Selected == true) { Trace.Warn(“Selected Item”, listItem.Value); } } 有没有办法把它变成一条线? 像我的伪代码在这里: string values = myListBox.SelectedItems; 我正在使用ASP.NET和C#3.5。 谢谢你的帮助!!

linq表达式不区分大小写

我正在利用这个项目来使用jqgrid来过滤和排序集合 。 缺少一个function是这个例子不是我需要的不区分大小写的搜索。 因此,如果用户键入“Test”,我希望它与“TEST”,“TeST”等匹配。 。 我有这样的代码: case WhereOperation.Equal: condition = Expression.Equal(memberAccessToString, filter); lambda = Expression.Lambda(condition, parameter); break; case WhereOperation.NotEqual: condition = Expression.NotEqual(memberAccessToString, filter); lambda = Expression.Lambda(condition, parameter); break; case WhereOperation.Contains: condition = Expression.Call(memberAccessToString, typeof(string).GetMethod(“Contains”), Expression.Constant(value)); lambda = Expression.Lambda(condition, parameter); break; 无论如何,下面的这些检查是不区分大小写的,所以“测试”将等于“测试” Expression.NotEqual Expression.Equal Expression.Call(memberAccessToString, typeof(string).GetMethod(“Contains”),

从LINQ表达式中提取sql查询

是否可以从LINQ查询中提取sql语句? 说,我有这个LINQ表达式。 string[] names = new string[] { “Jon Skeet”, “Marc Gravell”, “tvanfosson”, “cletus”, “Greg Hewgill”, “JaredPar” }; var results = from name in names where name.StartsWith(“J”) select name; alt text http://ruchitsurati.net/files/linq-debugging.png 在此语句之后,’results’仅保留LINQ表达式而不是由于LINQ查询的延迟执行而导致的结果。 我可以从’结果’中提取或生成LINQ查询,并从存储在’LINQ’中的查询中准备一个有效的SQL语句吗? 编辑 这是我的目标: 我们已经编写了自己的ORM。 我们每次需要进行数据库操作时都必须编写查询。 现在我们需要在DAL摆脱它。 我们希望在代码中编写LINQ表达式,它将针对我的ORM生成SQL语句,我们将在数据库上执行此SQL。 我是否愿意编写我的定制Linq提供商来做我需要的事情?

如何使用LINQ选择复合对象的所有后代

如何使用LINQ更好地使ComponentTraversal.GetDescendants() ? 题 public static class ComponentTraversal { public static IEnumerable GetDescendants(this Composite composite) { //How can I do this better using LINQ? IList descendants = new Component[]{}; foreach(var child in composite.Children) { descendants.Add(child); if(child is Composite) { descendants.AddRange((child as Composite).GetDescendants()); } } return descendants; } } public class Component { public string Name { […]

来自Enumerable.Range的A到Z的char列表

我想从Enumerable.Range中创建一个列表。 这段代码是否正确? SurnameStartLetterList = new List(); Enumerable.Range(65, 26).ToList().ForEach(character => SurnameStartLetterList.Add((char)character)); 或者有更好的方法来制作这种类型的列表吗?

Linq按日期获得数据组的总和

我的数据看起来像: read_date | T1 | T2 | 15.02.2000 | 2 | 3 | 16.02.2000 | 4 | 5 | 15.03.2000 | 2 | 3 | 16.03.2000 | 5 | 4 | 我想获得T1和T2的总和,如下所示: read_date | T1 | T2 | 02.2000 | 6 | 8 | 03.2000 | 7 | 7 | 我试着写这样的东西: var result = from […]