Tag: linq to entities

一个数据库请求中的Concat IQueryable集合

我使用entity framework。我需要concat两个集合。例如: IQueryable collection1 = context.Entiies.Where(predicate); IQueryable collection2 = context.Entiies.Where(otherPredicate); var result = collection1.concat(collection2).toList(); //1 var result = collection1.union(collection2).toList; //2 1和2变体都将在数据库中执行2个请求,因为这些方法需要IEnumerable作为参数。所以问题是我可以通过一个数据库调用来连接两个Iqueryble集合

延迟执行 – 字典输出

我有一个Linq查询,返回大约五十万行。 但是,由于我没有调用ToList(),执行仍然是延迟的。 到现在为止还挺好。 我需要一个来自此查询的Dictionary<int, List>输出,它将在Dictionary中返回大约100,000个条目(Distinct整数列值)。 我试过这样的事…… var myDictionary = new Dictionary<int, List>(); var myDictionaryOutput = MyQuery.Select(p => AddOrUpdateDictionary(myDictionary, p)); MyQuery是我的linq查询,AddOrUpdateDictionary是我写的自定义函数。 这会将每个条目投射到一个不是我想要的字典。 我只想要字典输出。 样本数据如下…… 1234 | 2015-08-24 | 2015-08-25 | null | 1234 | null | 2015-08-26 | null | 2345 | null | null | 2015-08-23 | 我希望输出是一个带有两个键的字典 (2行中的2个键来自500,000行的100,000个键), 1234 -> List{2015-08-24, 2015-08-25, 2015-08-26} and […]

LINQ查询论坛

我正在编写这个论坛,因为我是LINQ的新手,当用户点击主页面时遇到了这个问题。 我想要一个显示这样的论坛列表的表: Forum — Topics (count) — Posts (count) — LastPostUserId — LastPostTime 我有以下SQL表: Forums: ForumId (int32), Title (string), Description (string) ForumThreads: ThreadId (int32), ForumId (int32), UserId (guid), Subject (string), Views (int32), CreateDate (DateTime) ForumPosts: PostId (int32), ThreadId (int32), UserId (guid), Post (string), CreateDate (datetime) 谢谢…

如何将此Func转换为表达式?

我正在玩表达树,并试图更好地了解它们的工作原理。 我写了一些我正在使用的示例代码,希望有人可以帮助我。 所以我有一个有点凌乱的查询: /// /// Retrieves the total number of messages for the user. /// /// The name of the user. /// True if retrieving the number of messages sent. /// The total number of messages. public int GetMessageCountBy_Username(string username, bool sent) { var query = _dataContext.Messages .Where(x => (sent ? x.Sender.ToLower() : x.Recipient.ToLower()) == […]

Linq to Entities有多对多选择:如何强制生成JOIN而不是subselect子句?

首先使用EF DB我有两个具有多对多关系的实体(供应商,产品)。 entity framework不为关联表(SupplierProduct)创建实体,因为关联表仅包含强实体的主键。 我一直在为所有不提供给定产品的供应商提供以下查询: var q1 = context.Suppliers.Where(s=>!s.Products.Any(p=>p.Id == 1)); 生成的SQL使用与此类似的EXISTS依赖子查询: SELECT * FROM Suppliers s WHERE NOT EXISTS (SELECT 1 FROM SupplierProduct sp WHERE sp.SupplierId = s.Id && sp.ProductId = 1) 是否可以使用Linq to Entities方法语法生成在关联表上使用连接的查询? 即: SELECT DISTINCT s.* FROM SupplierProduct sp JOIN Supplier s ON s.Id = sp.SupplierId; WHERE sp.ProductId != 1 更新 […]

使用实体方法的结果来过滤lambda表达式或linq查询

我想根据使用其属性的函数的结果来过滤我的entites。 即。 我有这样的实体: public class Lorem { public int A {get;set;} public int B {get;set;} public int C {get;set;} public double DoMath(int externalValue) { // real implementation is longer and more complex if(A==B) { // some calculations return 0.2; } if(B==C) { // some calculations return 0.9; } else return 0; } } 现在我正在查询实体,我想只获得DoMath> 0的那些实体。 […]

entity framework – 由同一列中的多个条件选择 – 引用表

示例场景: 两个表: order和orderItem ,关系一对多。 我想选择至少有一个orderItem的价格为100的订单和至少一个价格为200的orderItem的订单。我可以这样做: var orders = (from o in kontextdbs.orders join oi in kontextdbs.order_item on o.id equals oi.order_id join oi2 in kontextdbs.order_item on o.id equals oi2.order_id where oi.price == 100 && oi2.price == 200 select o).Distinct(); 但是,如果这些条件是用户生成的呢? 所以我不知道会有多少条件。

将2个参数Lambda表达式转换为1个参数Lambda表达式(指定一个参数)

我有表达 Expression<Func> CanBeDrivenBy = (car, driver) => car.Category == ‘B’ && driver.Age > 18; 我想得到一些可以由一些司机驾驶的汽车 IQueryable cars = …; Driver driver = …; cars.Where(CanBeDrivenBy); // Fail, expecting Expression<Func> 所以我需要将Expression<Func>为Expression<Func> (指定驱动程序) 是的,我可以使用 cars.Where(c => c.Category == ‘B’ && driver.Age > 18); 但我需要能够动态改变表达式的解决方案。 我需要传递Expression(使用entity framework)

Linq to Entities的联盟订单

我有EDM模型联盟选择的问题。 我在db with uniqe Ids中有记录。 例如id列表:1,2,3,4,5,6,7,8,9 例如,我需要选择在#6之前记录#6和2记录,在#6之后记录2记录。 在选择结果中,它应该是4,5,6,7,8 我接下来做了这个: public IQueryable GetNextPrev(Int64 photoid, string userlogin) { var p1 = (from m in db.photos where m.id = photoid && m.userlogin == userlogin orderby m.id descending select m).Take(3).Skip(0); return (p1.Union(p2)); } 但是顺序与示例中的不同…… 谢谢您的帮助!

将派生属性包含到linq到实体查询中

这是一个基于名为Task的Poco类的简单项目: class Program { static void Main(string[] args) { using (MyDbContext ctx = new MyDbContext()) { // first query DateTime compareDate = DateTime.Now + TimeSpan.FromDays(3); var res = ctx.Tasks.Where(t => t.LastUpdate t.ShouldUpdate).ToList(); } } } public class MyDbContext : DbContext { public DbSet Tasks { get; set; } } public class Task { public int […]