linq-to-entities

NullReferenceException in EntityFramework, how come?

痴心易碎 提交于 2019-12-25 02:53:58
问题 Take a look at this query: var user = GetUser(userId); var sessionInvites = ctx.SessionInvites .Include("InvitingUser") .Include("InvitedUser") .Where(e => e.InvitedUser.UserId == user.UserId) .ToList(); var invites = sessionInvites; // Commenting out the two lines below, and it works as expected. foreach (var invite in sessionInvites) ctx.DeleteObject(invite); ctx.SaveChanges(); return invites; Now, everything here executes without any errors. The invites that exists for the user are being

How can I include more than one level deep in a LINQ query?

旧城冷巷雨未停 提交于 2019-12-25 02:32:47
问题 I have three SQL tables that are represented by classes and I would like to have Entity Framework 6 join these tables so I get all the details of the Exam , Test and UserTest tables where the UserTest.UserID is 0 or X . I have already set up a respository and this works for simple queries however I am unable to join the UserTest class in the LINQ at the bottom of the question. Here's my classes: public class Exam { public int ExamId { get; set; } public int SubjectId { get; set; } public

how do add a single entry for two tables using linq to entities

笑着哭i 提交于 2019-12-25 02:25:01
问题 I have table called product product_id product_Name product_Price product_Description product_image category_id another table category category_id category_name i have a new form with textboxes like txtprodname txtproddescrip txtproductprice picturebox1 txtcategoryname i am trying to add the new product to producttable by using following code i am using entity framework.. public byte[] imageToByteArray(System.Drawing.Image image) { MemoryStream ms = new MemoryStream(); image.Save(ms, System

Linq2Entities Equivalent Query for Parent/Child Relationship, With All Parents and Children, Filtering/Ordering Children

会有一股神秘感。 提交于 2019-12-25 02:22:18
问题 So the question is ridiculously long, so let's go to the code. What's the linq2entities equivalent of the following Sql, given entities (tables) that look like: Parent --- parent_id parent_field1 Child -- child_id parent_id child_field1 child_field2 The sql: select p.*, c.* from parent p inner join p on p.parent_id = child.parent_id where c.child_field1 = some_appropriate_value order by p.parent_field1 c.child_field2 L2E let's you do .include() and that seems like the appropriate place to

EF Query with conditional include that uses Joins

给你一囗甜甜゛ 提交于 2019-12-25 01:27:17
问题 This is a follow up to another user's question. I have 5 tables CompanyDetail CompanyContacts FK to CompanyDetail CompanyContactsSecurity FK to CompanyContact UserDetail UserGroupMembership FK to UserDetail How do I return all companies and include the contacts in the same query? I would like to include companies that contain zero contacts. Companies have a 1 to many association to Contacts, however not every user is permitted to see every Contact. My goal is to get a list of every Company

Linq to Entities, Inject Join Inside Query Expression

限于喜欢 提交于 2019-12-25 00:37:10
问题 I'm trying to improve the performance for some of our LINQ queries and there is one little thing that has a lot of space for improvement: joins. Almost all my queries have some join that is used to filter the result, but are not selected into de result. And these filtering conditions are optional... What I have today is something like: var q = from t1 in context.Set<T1>() where t1.mandatoryfilter >= 0 select t1; if (useFilter) { var q2 = from t1 in q from t2 in context.Set<T2>().Where(t2 =>

Why is Linq query setting my Arithabort options to false?

走远了吗. 提交于 2019-12-24 22:32:28
问题 I have code like this: using (var db = new MyDataContext()) { db.ExecuteStoreCommand("Set Arithabort on"); var q = AFairlyComplexQuery(db); // returns an IQueryable<> var result = q.ToList(); // Line 4 return result; } I was finding that this query was timing out. I ran SQL Profiler and grabbed the SQL and ran it in SSMS, and it came back in 7 seconds. From past experience, I've learned that this is invariably being caused by the Arithabort option being set to 0 , which is why I run that

ObjectCollection.Where(o => o.Related == EntityObject): “Unable to create a constant value”

巧了我就是萌 提交于 2019-12-24 21:17:53
问题 Given an EntityObject, I'd like an object-oriented way find all related items as part of my data-source query. The following produces the correct output, but brings all the rows over the wire to do it. Parent. // EntityObject Children. // EntityCollection Where(o => o.Gender == 'm'). // IEnumerable (local!) OrderBy(o => o.Age). // IOrderedEnumerable Skip(pages * pageSize).Take(pageSize); // (Inefficient paging!) I need to support a UI with this (filter using other criteria, sort, and paginate

Write a translatable method for LINQ TO Entities

╄→尐↘猪︶ㄣ 提交于 2019-12-24 18:43:33
问题 Using Entity Framework (LINQ to Entities) The following is working just fine. The expressions got translated to SQL var foos = ctx.Foos.Select(f => new { P1 = ctx.Bars.FirstOrDefault(b => b.SomeProp == "Const1" && f.X1 == b.Y), P2 = ctx.Bars.FirstOrDefault(b => b.SomeProp == "Const2" && f.X2 == b.Y), P3 = ctx.Bars.FirstOrDefault(b => b.SomeProp == "Const3" && f.X3 == b.Y), } The repetitive expression b.SomeProp == "..." && f.X* == b.Y is actually a simplified version of the real expression,

Write a translatable method for LINQ TO Entities

和自甴很熟 提交于 2019-12-24 18:43:04
问题 Using Entity Framework (LINQ to Entities) The following is working just fine. The expressions got translated to SQL var foos = ctx.Foos.Select(f => new { P1 = ctx.Bars.FirstOrDefault(b => b.SomeProp == "Const1" && f.X1 == b.Y), P2 = ctx.Bars.FirstOrDefault(b => b.SomeProp == "Const2" && f.X2 == b.Y), P3 = ctx.Bars.FirstOrDefault(b => b.SomeProp == "Const3" && f.X3 == b.Y), } The repetitive expression b.SomeProp == "..." && f.X* == b.Y is actually a simplified version of the real expression,