Convert anonymous type to new C# 7 tuple type

Of course, by creating the tuple from your LINQ expression: public (int id, string name) GetSomeInfo() { var obj = Query<SomeType>() .Select(o => (o.Id,o.Name)) .First(); return obj; } According to another answer regarding pre-C# 7 tuples, you can use AsEnumerable() to prevent EF to mix things up. (I have not much experience with EF, but … Read more

Howto use predicates in LINQ to Entities for Entity Framework objects

You don’t need LinqKit to do this. Just remember to use Expression<Func<MyEntity, bool>> instead of Func<MyEntity, bool> Something like this: public IQueryable<MyEntity> GetAllMatchedEntities(Expression<Func<MyEntity, Boolean>> predicate) { return _Context.MyEntities.Where(predicate); } You have to use Expression because Linq to Entities needs to translate your lambda to SQL. When you use Func your lambda is compiled to IL … Read more

How do I delete an object from an Entity Framework model without first loading it?

It is worth knowing that the Entity Framework supports both to Linq to Entities and Entity SQL. If you find yourself wanting to do deletes or updates that could potentially affect many records you can use the equivalent of ExecuteNonQuery. In Entity SQL this might look like Using db As New HelloEfEntities Dim qStr = … Read more

Group by Weeks in LINQ to Entities

You should be able to force the query to use LINQ to Objects rather than LINQ to Entities for the grouping, using a call to the AsEnumerable extension method. Try the following: DateTime firstDay = GetFirstDayOfFirstWeekOfYear(); var userTimes = from t in context.TrackedTimes.Where(myPredicateHere).AsEnumerable() group t by new {t.User.UserName, WeekNumber = (t.TargetDate – firstDay).Days / 7} … Read more

How to avoid Query Plan re-compilation when using IEnumerable.Contains in Entity Framework LINQ queries?

This is a great question. First of all, here are a couple of workarounds that come to mind (they all require changes to the query): First workaround This one maybe a bit obvious and unfortunately not generally applicable: If the selection of items you would need to pass over to Enumerable.Contains already exists in a … Read more

LINQ to Entities how to update a record

Just modify one of the returned entities: Customer c = (from x in dataBase.Customers where x.Name == “Test” select x).First(); c.Name = “New Name”; dataBase.SaveChanges(); Note, you can only update an entity (something that extends EntityObject, not something that you have projected using something like select new CustomObject{Name = x.Name}

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)