Tag: linq

LINQ中IN子句的位置

如何在SQL Server中创build一个类似于where的子句? 我自己做了一个,但任何人都可以改善这一点? public List<State> Wherein(string listofcountrycodes) { string[] countrycode = null; countrycode = listofcountrycodes.Split(','); List<State> statelist = new List<State>(); for (int i = 0; i < countrycode.Length; i++) { _states.AddRange( from states in _objdatasources.StateList() where states.CountryCode == countrycode[i].ToString() select new State { StateName = states.StateName }); } return _states; }

批量删除LINQ to Entities

有没有办法批量删除LINQ或LINQ-to-Entities中匹配给定查询的一堆对象? 我能find的唯一的引用是过时的,似乎很愚蠢的反复,并手动删除所有我想删除的对象。

C#Linq Group By多列

public class ConsolidatedChild { public string School { get; set; } public string Friend { get; set; } public string FavoriteColor { get; set; } public List<Child> Children { get; set; } } public class Child { public string School { get; set; } public string Name { get; set; } public string Address { […]

有没有一个很好的LINQ方法来做笛卡尔产品?

我有一个像这样的类结构: Person Dogs (dog 1, dog 2, etc) Puppies (puppy A, puppy B, etc) 有一个人 他有1..n条狗。 每只狗有1..n只小狗。 我想要一个所有可能的小狗组合的列表,每只狗取一只小狗。 例如: 狗1小狗A狗2小狗狗1小狗A狗2小狗B狗1小狗B狗2小狗狗1小狗B狗2小狗B 如果是在sql表中,我会做类似下面的“乘”表: select * from puppies a, puppies b where a.parent='dog1' and b.parent='dog2' 有没有一些linq-ish的方式来做这种事情? 非常感谢

仅支持初始化器,实体成员和实体导航属性

我得到这个例外: LINQ to Entities不支持指定的types成员“Paid”。 仅支持初始化器,实体成员和实体导航属性。 public ActionResult Index() { var debts = storeDB.Orders .Where(o => o.Paid == false) .OrderByDescending(o => o.DateCreated); return View(debts); } 我的模型类 public partial class Order { public bool Paid { get { return TotalPaid >= Total; } } public decimal TotalPaid { get { return Payments.Sum(p => p.Amount); } } Payments是一个包含字段金额的相关表,如果我删除Where子句显示有关付款的正确信息,任何线索有什么问题的代码查询工程? […]

无法创buildtypes的常量值在此上下文中仅支持基本types或枚举types

我得到下面的查询这个错误 无法创buildtypesAPI.Models.PersonProtocol的常量值。 在此上下文中仅支持基本types或枚举types 下面的ppCombined是PersonProtocolType一个IEnumerable对象,它由2个PersonProtocol列表的连接构成。 为什么这个失败? 我们不能在JOIN的SELECT里面使用LINQ JOIN子句吗? var persons = db.Favorites .Where(x => x.userId == userId) .Join(db.Person, x => x.personId, y => y.personId, (x, y) => new PersonDTO { personId = y.personId, addressId = y.addressId, favoriteId = x.favoriteId, personProtocol = (ICollection<PersonProtocol>) ppCombined .Where(a => a.personId == x.personId) .Select( b => new PersonProtocol() { personProtocolId = […]

扩展方法必须在非generics静态类中定义

我收到错误: 扩展方法必须在非generics静态类中定义 在线上: public class LinqHelper 这是基于Mark Gavells代码的助手类。 我真的很困惑这个错误是什么意思,因为我确信当我在星期五离开时它工作的很好! using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Linq.Expressions; using System.Reflection; /// <summary> /// Helper methods for link /// </summary> public class LinqHelper { public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property) { return ApplyOrder<T>(source, property, "OrderBy"); } public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string […]

用LINQ进行对象分页

你将如何在LINQ查询中实现分页? 其实暂时,我会满意,如果SQL TOPfunction可以被模仿。 不过,我相信无论如何,迟早会出现对全面分页支持的需求。 var queryResult = from o in objects where … select new { A = oa, B = ob } ????????? TOP 10????????

LINQ to SQL – 左外部连接多个连接条件

我有以下SQL,我正在尝试转换为LINQ: SELECT f.value FROM period as p LEFT OUTER JOIN facts AS f ON p.id = f.periodid AND f.otherid = 17 WHERE p.companyid = 100 我已经看到了左外连接的典型实现(即into x from y in x.DefaultIfEmpty()等),但是不确定如何引入其他连接条件( AND f.otherid = 17 ) 编辑 为什么AND f.otherid = 17条件部分JOIN,而不是在WHERE子句? 因为f可能不存在的一些行,我仍然希望这些行被列入。 如果在WHERE子句中应用条件,则在JOIN之后 – 那么我不会得到我想要的行为。 不幸的是: from p in context.Periods join f in context.Facts on […]

你将如何做一个“不在”查询与LINQ?

我有两个集合在两个集合中都有属性Email 。 我需要获得第一个列表中Email不存在于第二个列表中的项目列表。 使用SQL,我只是使用“不在”,但我不知道在LINQ中的等效。 这是怎么做的? 到目前为止,我有一个join,如… var matches = from item1 in list1 join item2 in list2 on item1.Email equals item2.Email select new { Email = list1.Email }; 但我不能join,因为我需要的差异和联接将失败。 我需要一些使用Contains或Exists的方式,我相信。 我还没有find一个这样做的例子。