linq-to-entities

LINQ to Entities / LINQ to SQL: switching from server (queryable) to client (enumerable) in the middle of a query comprehension?

a 夏天 提交于 2020-01-01 04:36:06
问题 In many cases, I want to do some filtering (and sometimes projection) on the server side and then switch to client-side for operations that the LINQ provider doesn't natively support. The naive approach (which is basically what I do now) is to just break it up into multiple queries, similar to: var fromServer = from t in context.Table where t.Col1 = 123 where t.Col2 = "blah" select t; var clientSide = from t in fromServer.AsEnumerable() where t.Col3.Split('/').Last() == "whatever" select t

LINQ - NOT selecting certain fields?

别等时光非礼了梦想. 提交于 2020-01-01 02:43:10
问题 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 query returns all the properties of my Image table in the DB. Under normal circumstances, this would be fine but these images contain a lot of binary data that takes a very long time to return. Basically, in it current state

LINQ generating SQL with duplicate nested selects

柔情痞子 提交于 2020-01-01 02:12:29
问题 I'm very new to the .NET Entity Framework, and I think it's awesome, but somehow I'm getting this strange issue (sorry for the spanish but my program is in that language, anyway it's not a big deal, just the column or property names): I'm doing a normal LINQ To Entities query to get a list of UltimaConsulta, like this: var query = from uc in bd.UltimasConsultas select uc; UltimasConsultas is a view, btw. The thing is that LINQ is generating this SQL for the query: SELECT [Extent1].[IdPaciente

Entity Framework Select new POCO without .ToList() first

家住魔仙堡 提交于 2020-01-01 01:53:10
问题 I'm creating an application with a service layer (WCF Website) and a Silverlight 4 Client. RIA Services are not an option, so we create intermediary classes to pass back and forth. For the purpose of this question let's assume I'm passing back and forth Tasty Food Objects. public class FoodData { public int Id { get; set; } public string Name { get; set; } public Tastyness TastyLevel { get; set; } } The EF Model is essentially the same class, a table with three basic fields (the Tastyness is

Entity Framework & LINQ To SQL - Conflict of interest?

丶灬走出姿态 提交于 2019-12-31 09:29:28
问题 I've been reading on the blogosphere for the past week that Linq to SQL is dead [and long live EF and Linq to Entities]. But when I read the overview on MSDN, it appeared to me Linq to Entities generates eSQL just the way Linq to SQL generates SQL queries. Now, since the underlying implementation (and since SQL Server is not yet an ODBMS) is still a Relational store, at some point the Entity framework has to make the translation into SQL queries. Why not fix the Linq to SQL issues (m:m

Alternative way to do FirstOrDefault in LINQ

血红的双手。 提交于 2019-12-31 05:04:06
问题 I've got a membership table that records whether a user is a member of a list. When an update to a user's membership occurs a new record is written and the previous records are left as is which allows a history of their memberships to be maintained. To get a user's membership status involves selecting their most recent entry. An example of some user list membership data is below. The aim is to find a LINQ expression that groups by list and user but only returns the row with most recently

Using an IQueryable in another IQueryable

十年热恋 提交于 2019-12-31 04:38:07
问题 I've an extension method, which returns an IQueryable, to get company products, I just want to use it in a IQueryable as a subquery, public static class DBEntitiesCompanyExtensions { public static IQueryable<Product> GetCompanyProducts(this DBEntities db, int companyId) { return db.Products.Where(m => m.CompanyId == companyId); } } And this is how I call it, using(var db = new DBEntities()) { var query = db.Companies.Select(m => new { CompanyName = m.Name, NumberOfProducts = db

IEnumerable vs IQueryable

我们两清 提交于 2019-12-31 03:52:07
问题 I have a query: topics.OrderBy(x => x.Replies.Any() ? x.Replies.OrderBy(y => y.PostedDate).Last().PostedDate : x.PostedDate); It sorts the topics collection by last reply, or if it has no reply then by its own post date. It works great and does its job well. However, it only works if topics is of type IEnumerable<Topic> . If I try to make it `IQueryable' I get an error saying the extension method Last() is unknown or something to that effect. Anyone know why? For some reason getting x.Replies

LINQ to Entities using the SQL LIKE operator

爱⌒轻易说出口 提交于 2019-12-30 20:17:45
问题 I have this: query = query.Where(s => s.ShowTypeDescription == showTypeDescription); several times for different variables in order to build dynamic SQL. How would I go about transforming the above to say: query = query.Where(s => s.ShowTypeDescription **LIKE** showTypeDescription); ? 回答1: If all you want is to find a substring within another string, the best way to do this is with the Contains method: query = query.Where(s => s.ShowTypeDescription.Contains(showTypeDescription)); Because the

Select Anonymous type with Dynamic Expression API

只谈情不闲聊 提交于 2019-12-30 10:59:14
问题 I'm using Dynamic Expression API ( System.Linq.Dynamic ) with LINQ to Entities. My LINQ query is below. var query = this.db.Products.AsQueryable() .Where(strCondition) .OrderBy("ProductNumber") .Select("new(ProductNumber, ProductDescription, ProductCategory.Name)"); Now that I have the "query", I don't know how to get the value of each of the field. string strTemp; foreach (var item in query) { strTemp = item.? } It's anonymous type so I can't really use strongly type to get the value. What