linq-to-entities

query and create objects with a one to many relationship using LINQ

三世轮回 提交于 2019-12-14 03:51:52
问题 In the DB, I have a two tables with a one-to-many relationship: orders suborders ----------- ----------- id id name order_id name I'd like to query these tables and end up with a list of order objects, each of which contains a list (or empty list) of suborder objects. I'd also like to do this in a single DB query so it performs well. In traditional SQL query land, I'd do something like (forgive the pseudocode): rs = "select o.id, o.name, so.id, so.name from orders o left join suborders so on

C#/ASP.NET lambda conversion

时光怂恿深爱的人放手 提交于 2019-12-14 03:27:29
问题 Somehow when I return this to my view: return View(db.Properties.ToList()); I get 3 properties, one of which has the location value of "Oregon". But in another method declared like this: public ViewResult Browse(string location) { return View(db.Properties.Where(p => p.location == location).ToList()); } I get 0 properties. I suspect the string "location" is getting converted to something strange somewhere, but I'm not sure in what way it would be getting converted. When I replace the lambda '

Linq to entities use `Func` to create property in a select statement that produces an anonymous object

回眸只為那壹抹淺笑 提交于 2019-12-14 01:49:37
问题 I am working on a simple text search method using linq to entities that I would like to reuse in several places that looks a bit like this: IQueryable<MyObject> query = db.MyObjects.Where(o => /* some criteria */); query = query .Select(o => new { value = o, search = o.Foo + " " + o.Bar.X + " " + o.Bar.Y }) .Where(o => o.search.contains("foo")) .Select(o => o.value); query = query.Where(o => /* some other criteria */); I would like to be able to turn the Select > Where > Select sequence into

Entity-Framework -> MySql gives 'Function evaluation timed out.'

倾然丶 夕夏残阳落幕 提交于 2019-12-14 01:21:55
问题 I having a weird problem with Entity Framework with MySql database. Here's the code that I've got. public class testbase { private testEntities db = new testEntities(); public IQueryable<post> GetRecords() { return db.record; } } Here record is a table in my database and this could should return all the rows in the table. I have only one row in there and when I do a db.record.Count(), I get 1. But when I try to retrieve the rows themselves I get 'Function Evaluation timed out'. What's

Recursive linq to get infinite children

本秂侑毒 提交于 2019-12-14 00:27:38
问题 I'm new to linq and am trying to find a way to return the parent and a List (children) and all those children, for a given parent. I have a Locations table with the fields LocationID , ParentLocationID , LocationName . A sample of the data could look like this: ABC --ABC1 --ABC2 ----DEF1 ----DEF2 ----DEF3 --ABC3 ----DEF4 ------GHI1 ------GHI2 ----DEF5 --ABC4 ... Given that data, if the selected parent is 'ABC', I want to return all the rows because all children are under it. However, if I

Loading relations in linq2entities automatically

半腔热情 提交于 2019-12-13 20:20:38
问题 When i have a relation between two entities in my model: [GroupMember] (*) ----- (1) [User] and tries to select items from this relation with LINQ: From entity in _user.GroupMember select entity I always get an empty result unless I load the relation first with following statement: _user.GroupMember.Load() Is there a way to avoid loading the relations like this? 回答1: If you have cascading relations, you can handle them with .Include("GroupMember.AnotherTable.YetAnotherTable") which is a

Using Func as parameter for LinqToEntities

一个人想着一个人 提交于 2019-12-13 16:26:26
问题 I have a Linq-Query to get my EF-Data. My query joins 4 tables and selects the result to a denormalized type. I will need this query very often but with different predicates. The List-ExtensionMethods (e.g. .Where() are working with a Func<T,bool> as a parameter and I wanted to do it the same - but I don´t find a way to access my predicate in my Method. public DenormalizedType GetData(Func<Thing, bool> predicate) { using (var dbContext = new MyDbContext()) { var myData = (from some in

Why getting data with Entity Framwork is slow? [closed]

偶尔善良 提交于 2019-12-13 09:29:43
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I am creating a Windows forms program with C# and Entity Framework. I want to fetch data from SQL database and do some processing after fetching it. Everything is good but the performance is too bad with too slow speed even with little data. My code is below and I want to know what is wrong with my code. I

How can i use DateTime.AddXXXX functions in a Linq-to-Entities query?

有些话、适合烂在心里 提交于 2019-12-13 08:36:58
问题 I am trying to use AddMonths in a query List<Entities.Subscriber> items = (from s in context.Subscribers where s.Validated == false && s.ValidationEmailSent == true && s.SubscriptionDateTime < DateTime.Now.AddMonths(-1) select s).ToList(); But I recieve an error : LINQ to Entities does not recognize the method 'System.DateTime AddMonths(Int32)' method, and this method cannot be translated into a store expression. Is there a way I can use this function inside my query? 回答1: The simplest fix to

How to requery updated values immediately after update procedure runs

梦想的初衷 提交于 2019-12-13 08:21:41
问题 As far as I can tell, changes made to the database by the following code to "MySite" are immediate: public List<vcData> UpdateDisplayAndUrl(List<vcData> vcDataList) { foreach (vcData vcData in vcDataList) { _entities.ExecuteStoreCommand("UPDATE vcData SET DisplayItem = {0}, DisplayUrl = {1} WHERE ID = {2} ", vcData.DisplayItem, vcData.DisplayUrl, vcData.ID); } return GetTableData(); } What I would like to do is to immediately return the newly updated records for further processing,