linq-to-entities

Null coalesce not working in LINQ query

99封情书 提交于 2021-02-18 12:45:31
问题 Take this: int? item1 = null; int? item2 = null; someObjectList.Where(x => x.SomeItem1 == (item1 ?? x.SomeItem1) && x.SomeItem2 == (item2 ?? x.SomeItem2) ); Where someObjectList is not empty and SomeItem1 and SomeItem2 is null in all the objects in the list. Why is it returning nothing? EDIT: My Code: public void GetPlacementsByMaterial(long clientMaterialID) { ClientMaterial clientMaterial = ((ApplicationEntityModel)NavigationItem.ObjectContext).ClientMaterial.FirstOrDefault(x => x

Null coalesce not working in LINQ query

穿精又带淫゛_ 提交于 2021-02-18 12:45:08
问题 Take this: int? item1 = null; int? item2 = null; someObjectList.Where(x => x.SomeItem1 == (item1 ?? x.SomeItem1) && x.SomeItem2 == (item2 ?? x.SomeItem2) ); Where someObjectList is not empty and SomeItem1 and SomeItem2 is null in all the objects in the list. Why is it returning nothing? EDIT: My Code: public void GetPlacementsByMaterial(long clientMaterialID) { ClientMaterial clientMaterial = ((ApplicationEntityModel)NavigationItem.ObjectContext).ClientMaterial.FirstOrDefault(x => x

Writing an extension method for Entity Framework

别说谁变了你拦得住时间么 提交于 2021-02-08 10:13:11
问题 I would like to have an extension method called FirstOrDefaultCache() which would check dbContext.EntityName.Local.FirstOrDefault(condition) , and only if that is null, check dbContext.EntityName.FirstOrDefault(condition) . I got the following from another post, which works okay: public static TEntity FirstOrDefaultCache<TEntity>(this DbSet<TEntity> queryable, Expression<Func<TEntity, bool>> condition) where TEntity : class { return queryable .Local.FirstOrDefault(condition.Compile()) // find

linq->SQL orderby descending not working

巧了我就是萌 提交于 2021-02-04 16:27:29
问题 var Customer = (from c in DNAContextSQL.Customers where c.LastName != "" orderby c.PKID_Customer descending select new { c.PKID_Customer, c.OrganizationName, c.FirstName, c.LastName, c.Phone, c.Extension }).Distinct().ToList(); I know this is basic. I can't find any good reason why it's not working though. The queries sent to SQL Profiler don't seem to have an order by clause in them. Any ideas? I can get it to work with .OrderByDescending(...) but would like to know the reason behind this

Build Expression tree for LINQ to Entities Where clause

心已入冬 提交于 2021-01-28 14:13:55
问题 I want to be able to write the following code for a LINQ to Entities query (EF6) Repo.ContactRelations.WhereActive() .Select(r => new ContactModel { Id = r.Contact.Id, FirstName = r.Contact.FirstName, LastName = r.Contact.Surname, WorkEmail = r.Contact.WorkEmail }) Without the WhereActive() method, I would have to repeat the following expression in numerous places: Repo.ContactRelations.Where(c => c.EndDate == null || c.EndDate > DateTime.Today) I tried to write a simple extension method, but

Entity Framework ObjectQuery order by date on many to many relation

烈酒焚心 提交于 2021-01-28 05:48:02
问题 I have an ObjectQuery like that: ObjectQuery<Car> query = GetCarQuery(); than i want to order by property Date on related entity Race , somehting like query = (ObjectQuery<Car>)query.OrderBy(x=> x.Races.Date); query.Skip((page - 1) * rows).Take(rows).ToList(); was trying to go like that: query = (ObjectQuery<Car>)query.OrderBy(i => i.Races.OrderBy(x=> x.Date)); query.Skip((page - 1) * rows).Take(rows).ToList(); //error here and got an error when query executed DbSortClause expressions must

Convert.Int64 Is Not Reconized LINQ To Entities

狂风中的少年 提交于 2021-01-27 20:11:06
问题 I am getting the following exception: LINQ to Entities does not recognize the method 'Int64 ToInt64(System.String)' method, and this method cannot be translated into a store expression. I had long.Parse(ProjectID.ToString()) and I see the suggestion was to use Convert.ToInt64 but I am still getting the same exception string projID = ProjectFileID.ToString(); var d = (from f in context.FileInfo where f.ID == Convert.ToInt64(projID) select (f)); 回答1: Just do the conversion outside the query, so

Using Function logic in LINQ Query net core 3

橙三吉。 提交于 2021-01-27 20:08:00
问题 I have the following enum: public enum WorkType { Type1, Type2, Type3, Type4, Type5, Type6 } and a class public class Work { public WorkType Type {get; set;} .... } and an extension method: public static partial class WorkTypeExtensions { public static bool IsHighValueWork(this WorkType value) { switch (value) { case WorkType.Type1: case WorkType.Type2: return true; default: return false; } } } and SQL Linq query public List<Work> GetHighValueWork() { var query = Context.Work.Where( w => w

Subquery in Where Clause of LINQ statement

╄→гoц情女王★ 提交于 2021-01-01 09:57:13
问题 So I tried to follow this example to have a sub-query in the where clause of this LINQ query. var innerquery = from app in context.applications select new { app.app_id }; IEnumerable<postDatedCheque> _entityList = context.postDatedCheques .Where(e => innerquery.Contains(e.appSancAdvice.application.app_id)); The objective was to select those records from postDatedCheques that have app_id in applications table. But I am getting following erros inside the where clause: Delegate 'System.Func'

Remove duplicate records using LINQ [duplicate]

十年热恋 提交于 2021-01-01 03:38:56
问题 This question already has answers here : LINQ: Distinct values (7 answers) Closed 5 years ago . I want to use linq to object to remove duplicate records. I want that final list will contain unique records with the latest Date. I have the following list AId BId C(date) **1 2 24/5/2015** 3 6 24/5/2015 **1 2 23/5/2015** (Need To Remove) I want to remove record 3. Any Advise? 回答1: You can use GroupBy and then Select with OrderByDescending to order the dates. public class Item { public int Aid