linq-to-entities

Linq To EF : How to filter using Non-Primitive types

ぐ巨炮叔叔 提交于 2019-12-21 20:37:06
问题 public class Person { public int ID { get; set; } public int Job { get; set; } public string Name { get; set; } } List<Person> personsOfInterest = GetPersonsOfInterest(); PersonEntities personEntities = new PersonEntities(); var filteredPersons = personEntities.Where(p => personsOfInterest.Any(poi => poi.Job == p.Job && poi.Name == p.Name)); The above code generates a NotSupportedException, because Linq to Entities does not support referencing non-scalar variables( Person ). how can I resolve

How do I delete one or more rows from my table using Linq to Entities *without* retrieving the rows first?

我与影子孤独终老i 提交于 2019-12-21 20:17:07
问题 I understand I can map a delete stored procedure to the delete method for a particular type. However, this requires passing in a retrieved object to my context's DeleteObject method. This is bad enough, but what if I want to delete 2000 rows? Can I do this with Linq to Entities without first retrieving those 2000 rows from the database and going through a loop calling DeleteObject ? If such functionality does not exist in Linq to Entities, and you know this to be the case, then please just

LastIndexOf in LINQ to Entities

心已入冬 提交于 2019-12-21 12:36:32
问题 I'm using LINQ to Entities to fetch elements from a MSSQL data base and I need them sorted by the last index of a string: var data = db.GetEntities(). OrderBy(e=>e.StringProperty.LastIndexOf("Foo")).ToList() However, LINQ to Entities does not support LastIndexOf . I've tried searching for similar questions but all I've found was this which does not address my issue (ordering). Searching on MSDN did not yield any results. What would be the simplest way to accomplish this using LINQ to Entities

Creating a dynamic Linq select clause from Expressions

拟墨画扇 提交于 2019-12-21 05:22:06
问题 Let's say I have defined the following variables: IQueryable<MyClass> myQueryable; Dictionary<string, Expression<Func<MyClass, bool>>> extraFields; // the dictionary is keyed by a field name Now, I want to tack on some dynamic fields to the IQueryable, so that it returns an IQueryable<ExtendedMyClass> , where ExtendedMyClass is defined as: class ExtendedMyClass { public MyClass MyObject {get; set;} public IEnumerable<StringAndBool> ExtraFieldValues {get; set;} } class StringAndBool { public

Extension method in where clause in linq to Entities

不问归期 提交于 2019-12-21 05:06:17
问题 In linq to Entities we needed a method that works like "sql like". We have implemented our own extension method to IQueryable, because contains method not work for us because doesn't accept patterns like '%a%b%' The created code is: private const char WildcardCharacter = '%'; public static IQueryable<TSource> WhereLike<TSource>(this IQueryable<TSource> _source, Expression<Func<TSource, string>> _valueSelector, string _textSearch) { if (_valueSelector == null) { throw new ArgumentNullException

IQueryable Lambda Projection Syntax

前提是你 提交于 2019-12-21 04:59:11
问题 I have an IQueryable whose Entity Framework 4 objects I would like to project to their DTO equivalents. One such object 'Person' is an EF4 class, and the corresponding POCO PersonP is a class I've defined. I am using Automapper to map between them. However, when I try the following code: IQueryable<Person> originalModel = _repo.QueryAll(); IQueryable<PersonP> projection = originalModel.Select(e => Mapper.Map<Person, PersonP>(e)); The projection generates this error at runtime: LINQ to

7-second EF startup time even for tiny DbContext

自闭症网瘾萝莉.ら 提交于 2019-12-21 03:49:32
问题 I am trying to reduce the startup time of my EF-based application, but I find that I cannot reduce the amount of time taken for an initial read below 7 seconds even for a single-entity context. What's especially strange is that this time is not context-type specific. Can anyone explain what causes these slow times and/or how I can get things to run faster? Here's the complete sample code: In my database, I have a table named se_stores with a primary key column AptId: // a sample entity class

How do you do a SQL style 'IN' statement in LINQ to Entities (Entity Framework) if Contains isn't supported?

筅森魡賤 提交于 2019-12-21 03:39:49
问题 I'm using LINQ to Entities (not LINQ to SQL) and I'm having trouble creating an 'IN' style query. Here is my query at the moment: var items = db.InventoryItem .Include("Kind") .Include("PropertyValues") .Include("PropertyValues.KindProperty") .Where(itm => valueIds.Contains(itm.ID)).ToList<InventoryItem>(); When I do this however, the following exception is thrown: LINQ to Entities does not recognize the method 'Boolean Contains(Int64)' method, and this method cannot be translated into a

Confused about passing Expression vs. Func arguments

送分小仙女□ 提交于 2019-12-21 03:20:09
问题 I'm having some trouble understanding the differences between how Expressions and Funcs work. This problem turned up when someone changed a method signature from: public static List<Thing> ThingList(Func<Thing, bool> aWhere) To public static List<Thing> ThingList(Expression<Func<Thing, bool>> aWhere) Which broke my calling code. The old calling code (which worked) looked like this: ... object y = new object(); Func<Thing, bool> whereFunc = (p) => p == y; things = ThingManager.ThingList

Cannot implicitly convert type 'System.Collections.Generic.List<T>' to 'System.Linq.IQueryable<T>'

怎甘沉沦 提交于 2019-12-21 02:28:34
问题 I am trying to create a query in my domain service (VS 2010 Silverlight Business Application) that returns the results from inspection readings that came out as a specific value, my database is set up as: Locations a) Inspections b) InspectionItems c) InspectionReadings a) Areas b) Inspections c) InspectionItems d) InspectionReadings So, as you can see, there are inspection readings for locations under areas and locations. I have a POCO called name StatusList: public class StatusList { [Key]