linq-to-entities

Entity Framework - "All" method

The All method is supposed to evaluate the argument against all elements in the list. It works ok in regular Linq but when I try to use it with EF it throws an error ("Unable to create a constant value of type 'Closure type'. Only primitive types (for instance Int32, String and Guid) are supported in this context. ") Example: var myLi...

Entity Framework and Linq to Entities and .Include() few tables, or maybe ..

Hi. i've got a database with a table named PropertyValues where i store every value i need to describe some properties of my database table rows. For example, table Products which looks like this : ID OrderID //Products table is related with Order table ProductName ProductType_ID // ID of PropertyValues table which describes...

How to create a LINQ expression from an Entity Framework navigation property?

I have the following bit of code: Expression<Func<Subscription, Service>> service2= subscription => (from relationship in subscription.ChildRelationships select relationship.SecondService).FirstOrDefault(); It creates an expression that I can use later as part of a query with the entity framework. The actual code I'm using has a w...

Is there a good way to detect empty results in a Linq-To-Entities query?

The only way I know of is awkward: 'check for empty return Dim count As Integer = (From s In myEntity.employee Where employeeID = myEmployeeIDVariable).Count 'If there is a record, then process If count > 0 Then Dim r = (From s In myEntity.employee Where employeeID = myEmployeeIDVariable).First() . . . do stuff . . . End If ...

Working with the ObjectQuery Single Enumeration Challenge in Linq-To-Entities Entity SQL

I'm working on modifying this example: Using advWorksContext As New AdventureWorksEntities ' Call the constructor that takes a command string and ObjectContext. Dim productQuery1 As New ObjectQuery(Of Product)("Product", advWorksContext) Dim result As Product For Each result In productQuery1 Console.WriteLine("P...

Challenge: Getting Linq-to-Entities to generate decent SQL without unnecessary joins

I recently came across a question in the Entity Framework forum on msdn: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bb72fae4-0709-48f2-8f85-31d0b6a85f68 The person who asked the question tried to do a relatively simple query, involving two tables, a grouping, order by, and an aggregation using Linq-to-...

Ado.Net entity framework, linq: select multiples tables

Hello! I use these sentence in C# to retrieve data from tables DetalleContenido and Archivo: var detallesContenido = from contenido in guiaContext.Contenido where contenido.PuntoInteres.id_punto == puntoInteresID from dc in contenido.DetalleContenido where dc.Idioma.ds_idioma == idiomaCliente select dc; The relati...

LINQ - NOT selecting certain fields?

I have a LINQ query mapped with the Entity Framework that looks something like this: image = this.Context.ImageSet .Where(n => n.ImageId == imageId) .Where(n => n.Albums.IsPublic == true) .Single(); This returns a single image object and works as intended. However, this quer...

Trying to develop a new extension method

Hi, I'm using the Entity Framework and I developed this extension method: public static IQueryable<TResult> Like<TResult>(this IQueryable<TResult> query, Expression<Func<TResult, string>> field, string value) { var expression = Expression.Lambda<Func<TResult, bool>>( Expression.Call(field.Body, typeof(string).GetMethod("Con...

LINQ query for a forum

Hi, I'm coding this forum and since I'm new to LINQ I ran into this problem when the user hits the main page. I want a table displaying a list of forums like this: Forum --- Topics (count) --- Posts (count) --- LastPostUserId --- LastPostTime I have the following SQL tables: Forums: ForumId (int32), Title (string), Description (str...

Why does LINQ to Entities not support SingleOrDefault()?

Any ideas why LINQ to Entities doesn't support SingleOrDefault() but instead ask to use FirstOrDefault()? Will SingleOrDefault() functionality be replaced? By what? ...

Difference between linq to entities and linq to sql

Possible Duplicate: Entity Framework vs LINQ to SQL What is the difference between linq to entities & linq to sql? Why linq to entities came into picture? ...

how to pass an entitykey(reference) from a view to the controller

I had a working page with linq to sql and now I'm currently converting it into linq to entity My test project contain 2 tables, contact(id,name,email) and comments(id,contactid,comment) when I want to create a new comment, I put in the view in a hidden field the contactid and it was submitting with everything else on the submit button ...

Things to be aware of when moving from LINQ to SQL --> LINQ to Entities

I have used LINQ to SQL successfully on an number of projects recently, but am keen to move to LINQ to Entities since this looks like the area that will be receiving more investment from Microsoft as they move to .NET 4.0 and beyond. Before doing so I am keen to find out what things LINQ to SQL has that are missing from LINQ to Entities...

OnValidate() and LINQ to Entities

I would like to implement some business rule validation like Scott Guthrie did in his MVC Nerddinner tutorial (http://nerddinnerbook.s3.amazonaws.com/Part3.htm), but I'm running into a problem trying to do so. Scott was using Linq to SQL in his tutorial. He creates partial classes for his data objects, and then he implements a partial ...

Adding a random Guid column to a Linq to Entities query to grab random records

I've found some articles about using the RandomView view and the GetNewID function to pull back randomized records, but they are using this method with Linq to SQL which allows Functions and Stored Procs to be used with no return value or a scalar return value. From what I understand, a Stored Proc has to been returned as one of the Enti...

Custom Generic GetTable

For a DataContext I'm working on, I don't want to load the Tables untill their needed. I want to make a little method that would check if a certain Table is loaded before loading it, but I end up having n Methods doing the same thing : private void Load(ref Table<Order> Orders) { if (Orders == null) Orders = this.GetTable<Or...

Linq to Entities

I built an Entity Framework (v1) model which includes inheritance, like this: Base class: Issue <- Complaint <- CustomerQuery etc. (6 derived classes) These classes map to corresponding tables in SQL Server. I was impressed with the way that I could then use LINQ to entities to do creation - EF figures out that it must put some data...

Linq-to-entities Include method doesn't load related records.

Hi, I've got 3 tables in my DB: InvoiceDetailLineType, InvoicingCategory, CalculationRule. InvoiceDetailLineType has foreign keys to both InvoicingCategory and CalculationRule. I want to retrieve a list of all InvoiceDetailLineType objects for a given category and include the related CalculationRule and InvoicingCategory objects. Her...

Populating a generic ViewModel for building reports

I need to generate several reports, and many of them will simply be a table of entities from my database. The entities could be of any type, and I won't always need the entire entity. My current approach is to create a ViewModel that contains a field of type List<List<string>>, which represents my table, where each row is a List of cel...