linq-to-entities

Is there a not in sub-query for Linq to Enties as in this T-SQL

无人久伴 提交于 2019-12-22 10:08:39
问题 Assume the classic self-referencing Employee table where each employee may have at most one ReportsTo, as created with this T-SQL snippet and sample data: create table Employees ( Id int identity primary key, Name nvarchar(30), Region nvarchar(10), ReportsTo int null foreign key(ReportsTo) references Employees(Id) ) insert into Employees values('Boss','HO',null) insert into Employees values('Underling', 'HO', (select Id from Employees where Name='Boss')) insert into Employees values('Self

Use result of entity's method for filtering with lambda expression or linq query

三世轮回 提交于 2019-12-22 09:57:13
问题 I would like to filter my entites depending on result of a function working with their properties. ie. I got entity like this: 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; } } Now I am querying entities and I would like to get only those with DoMath

LINQ to Entities, join two tables, then group and take sums of columns from both tables

守給你的承諾、 提交于 2019-12-22 08:43:58
问题 As the title says, my goal is to JOIN two tables (target and transaction) on several columns, then group the result of that join and sum the values of columns from BOTH tables. The following query only allows access to columns from the FIRST table in the join! var actualsVsTargets = (from target in ObjectContext.PipelineTargets join transaction in ObjectContext.Transactions on new { target.Year, target.Quarter, target.StateID, target.ProductGroup.TeamId } equals new { transaction.Year,

Converting ESQL to LINQ to Entities. Sort by related entities

蹲街弑〆低调 提交于 2019-12-22 08:29:44
问题 I am using EF + RIA and unfortunately meet some problems with sorting by related entities. For such purpose there is ESQL query that I implemented (found only this solution): var queryESQL = string.Format( @" select VALUE ent from SomeEntities as ent join Attributes as ea ON ea.EntityId = ent.Id where ea.AttributeTypeId = @typeId order by ea.{0} {1}", columnName, descending ? "desc" : "asc"); var query = ObjectContext.CreateQuery<SomeEntity>(queryESQL, new ObjectParameter("typeId",

Entity Framework 4 Abstract Model - How to Programatically Eager-Load Navigational Properties?

∥☆過路亽.° 提交于 2019-12-22 08:24:17
问题 I have an EF4 Model that is built with abstract entities/classes: Notice how State entity has a navigational property called Country . Note: I have lazy-loading disabled, so i must eager-load on demand. Now, if i have the following method: public Location FindSingle(int id) { return _repository.Find().WithId(id).SingleOrDefault(); } This does not return any associations by default. But how can i dynamically eager-load the associations when i explicitly want to? I cannot do this: return

Entity Container and Model generation in different assemblies

时光毁灭记忆、已成空白 提交于 2019-12-22 08:19:47
问题 I'm doing some refactoring and am trying to reuse my genertated entity models. My application has a few assemblies, one being my outward facing public types (API) and one containing implementations of providers (such as the log). I'd like to split the generation of the entities and models so that the entities will be in the API assembly and the container will be in the implementation assembly. Is this possible? Is possible. This is how I did it. Assembly A Database.EDMX Models.TT Models.cs

Union order in Linq to Entities

瘦欲@ 提交于 2019-12-22 06:56:27
问题 I have a problem with EDM model Union select. I have the records in db with uniqe Ids. For example id list: 1, 2, 3, 4, 5, 6, 7, 8, 9 I need to select, for example, record #6 and 2 records before #6 and 2 records past #6. In select result it should be 4,5,6,7,8 I made this next way: public IQueryable<photos> 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(2).Skip(0); var p2 =

What is the return type for a anonymous linq query select? What is the best way to send this data back?

时光总嘲笑我的痴心妄想 提交于 2019-12-22 06:39:50
问题 This is a basic question. I have the basic SL4/RIA project set up and I want to create a new method in the domain service and return some data from it. I am unsure the proper easiest way to do this.. Should I wrap it up in a ToList()? I am unclear how to handle this anonymous type that was create.. what is the easiest way to return this data? public IQueryable<ApplicationLog> GetApplicationLogsGrouped() { var x = from c in ObjectContext.ApplicationLogs let dt = c.LogDate group c by new { y =

Order execution of chain linq query

旧时模样 提交于 2019-12-22 04:31:35
问题 Is there a difference in this code? var query = DbContext.Customers .Where(<condition>) .Include("Address"); And var query = DbContext.Customers .Include("Address") .Where(<condition>); It's deffered query, and I don't know, is it equivalent? Or in the second case where is executed after Include ? Thanks. 回答1: For EF order prior to the select does not matter. The LINQ query is converted to a SQL query and run and the SQL query optimizer does not care about the order of the original query. As

EF: Cross edmx (same DB) linq-to-entites query

家住魔仙堡 提交于 2019-12-22 00:24:51
问题 How can I use two EDMXs, in separate assemblies, yet above the SAME database, to create a linq-to-entities query that uses them both? E.g. This is what I am trying to do: using (var context1 = new Entities1()) { using (var context2 = new Entities2()) { var items2 = context2.Items.Where(item2 => item2.Color == "Red"); var query = context1.Items.Where(item => items2.Any(item2 => item2.PainterId == item.PainterId)); } } > This results in NotSupportedException. Message: "The specified LINQ