linq-to-entities

What libraries do i use for compiling a linq to entities query

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using ///what here public static class CompiledQueries { public static Func<DataContext, int, IQueryable<Foo>> getFoo = CompiledQuery.Compile( (DataContext db, int ixFoo) => (from f in db.Foo wher...

Linq to entities insert EF 1

I have a 2 Entity : Person (Id, CategoryId) and Category(Id). Now when I try to insert a new Person with only the Id and CategoryId it throws an exception about the Person having a relationship with Category. The following code doesn't work: Person newPerson = new Person(); newPerson.Id = 1; newPerson.CategoryId = 1; context.AddToPer...

Can the where-condition in this LINQ-query be optimized?

Hey! I have this LINQ-query: bool? a, b, c; from i in _ctx.SomeEntitySet where (a == null ? true : i.IsA == a) && (b == null ? true : i.IsB == b) && (c == null ? true : i.IsC == c) select i; I only want to take the condition IsX == x in...

Why won't this LINQ join statement work?

I have this LINQ-query: // types... LinkedList<WeightedItem> itemScores = new LinkedList<WeightedItem>(); var result = from i in _ctx.Items join s in itemScores on i.Id equals s._id orderby s._score descending select new ItemSearchResult(i, s._score); // this fails: ...

Testing if a value is contained within a Dictionary<TKey, List<TValue>>

I need to determine if any of the Lists contained in the Dictionary contain the specified value. I'm new to LINQ, so is the following the correct way to achieve this? Dictionary lotsOfStuff = new Dictionary<string, List<string>>(); string searchString; // populate lotsOfStuff and searchString... // detemine if any of the values of lot...

Linq Query slow on host but not on dev machine

I'm running ASP.NET MVC 2 Preview 2 (With VS 2010 Beta 2) using Entity Framework. Earlier yesterday, for some unknown reason, a single page in my app started to load very slowly on my host. The problem is, this only occurs on my host and I haven't changed anything for it to load slow. Here is the action that is loading very slow: pub...

Entity Framework V1, PredicateBuilder, Linq to Entities failed to resolve method

Helo, i have the following code which should filter entities. Half of the code is working by i'm trying to refactor my code to get some sort of 'subfilters'. Basically i have the following call to filter a Users entity collection: var result = ctx.GetUsers().WithGroups("Administrators","Users").ToList(); WithGroups has the followin...

Linq throws an exception when nothing to return

I am using the following code to extract data from my database using entities. If a record isn’t found it throws the following exception “Object reference not set to an instance of an object.” I can catch this to stop it causing problems but would rather modify the code to not have the problem. Can I change the Linq query so that it is m...

Displaying data from multiple tables in a view as a list - ASP.Net MVC

Hi, I have the following two tables (basic outline): Tbl_CategoryType ID LevelID Description Tbl_Levels ID Name Basically, I want to present all of the information in the Tbl_CategoryType table while referencing the Tbl_Levels.Name data based on the Tbl_CategoryType.LevelID number. I have tried using a join in my repository as below...

Retrieving a tree structure from a database using LINQ

I have an organization chart tree structure stored in a database. Is is something like ID (int); Name (String); ParentID (int) In C# it is represented by a class like class Employee { int ID, string Name, IList < Employee> Subs } I am wondering how is the best way to retrieve these values from the database to fill up the C# Obje...

.NET EntityStoreSchemaFilterEntry filter patterns

First question to SO, I hope I'm doing this right. ;) Regarding System.Data.Entity.Design.EntityStoreSchemaFilterEntry : I'm looking for some detailed documentation on this class. The MSDN docs have nothing but an indication of what properties exist and their data types. I want to create a well-defined list of filters for EntityStoreS...

How can we optimize this linq to entity query to decrease the response time ?

IQueryable<WebEvent> mySearch = eventDC.GetBooks() .Where(p => p.Price.Any(d => d.EventDatetime.Month == fromDate.Month && d.EventDatetime.Year == fromDate.Year)) .WithGroup(groupId) .OrderBy(p => p.Price.Where(r => r.Datetime >= fromDate) .Or...

How to return many Child Collections Count() in a single entity framework query.

I have two entities, Parent and Child, in Entity Framework. The parent has a collection of Child entities. In my query, I want to return only the Parent entities (the fully typed EF types) and also the Count() of the Child entities (this could be set to a property on the Parent), but I only want to do this in one call to the database, ...

Given LINQ to Entities does not support "Custom methods" how do you stay DRY?

I have run into this problem: Custom Methods & Extension Methods cannot be translated into a store expression Basically I have some complicated LINQ queries, so wanted to break them down into subqueries which are implemented as methods that return IQueryables. My hope was then these IQueryables could be composed together in a LINQ stat...

ASP.NET Entity Framework NotSupportedException

I'm using LINQ to Entities in the Data layer of my application, but am getting hammered by a NotSupportedException in a call to results.ToList(). Here is the function causing the exception: public List<Organization> GetByLocation(Location l) { using (Entities entities = new Entities()) { var results =...

How to Define a Linq Extension Method

I am trying to run the code in there selected answer but can't figure it out link text Here is my code: using System; using System.Data; using System.Data.Objects; using System.Configuration; using System.Linq; using System.Linq.Expressions; using System.Collections; using System.Collections.Generic; using System.Web; using System.Web....

Retrieve only base class from Entity Framework

If I have three classes in entity framework. class Base {} class Left : Base {} class Right : Base {} and I call DBContext.Bases.ToList(); This returns all instances of Base fully typed into their associated inherited types, as some people have noticed, the performance of EF on large inheritance structures is not great to say the l...

How can I maintain deferred execution with EF while projecting into another type?

I'm using EF 4 and I need to find a way to maintain deferred execution and project into another type. This is my existing code: AppointmentRepository appointmentRepository = new AppointmentRepository(); var appointmentGridItems = from a in appointmentRepository.ListAppointments() select new AppointmentGridIte...

How to: A ListBox from two sources?

I am using EF with WPF. How should I create a ListBox that shows both Contacts and Persons? My question is rather how to retrieve it and create the CollcetionViewSource(s). I know I will have to use ItemTemplateSelector, that's less what I care, what I really care is the retrieval, but any tips on the representation will be welcommed as...

How is the performance of entity framework 4 vs entity framework 3.5?

I have one query on my page that takes at least a half second to execute using EF 3.5. When I used a stored procedure the speed was noticably faster. It is a very complex query. Will there be any performance improvements in the upcoming EF 4.0? And does EF 4.0 really beat out 3.5 performance wise? ...