linq-to-entities

linq to entities how to create database

拈花ヽ惹草 提交于 2019-12-13 04:19:01
问题 I'm new to linq to entities. In linqtosql you could do something like: datacontext.CreateDatabase() to generate a database from the model in sql. I'm wanting to do the same thing in linq to entities. Say I have a class MyEntities which inherits from ObjectContect is there a similiar method? 回答1: Try this // Create a builder and configure it var builder = new ContextBuilder<MyContext>(); … // Create a context var mycontext = builder.Create(sqlConnection); // Prepare the Context if (!myContext

using Lambda expression for implementing a query in many-to-many relation tables

情到浓时终转凉″ 提交于 2019-12-13 03:36:59
问题 According to the picture, I've designed 3 tables that are Article, Tag and ArticleTag. ArticleTag is a associated table between Article and Tag table. Now, I want to use Lambda expression to create a query that obtains articles according to TagId. I've used Entity Framework 6.x First I got TagId according to tagTitle by a query. // tagTitle is a parameter that passed to a method. // isActiveTag is a parameter that passed to a method. int currentTagId = DataContextFactory.GetDataContext()

LINQ - SQL Script Changing based on JOIN Order

烂漫一生 提交于 2019-12-13 03:29:34
问题 This is just strange behavior to me. LINQ transforming my statements creating bizarre execution plans. It's adding sub-queries when I don't ask it to and it's including or excluding joins in this sub-query based on the order of my joins, which leaves me scratching my head. At this point it's impossible for me to optimize this script based on the unpredictable nature of LINQ to Entities. SETUP //define global filter Expression<Func<Contract_SeqExecution, bool>> globalFilter = r => r

“Or” Together Expression<Func<EntityFramework Entity, bool>> in .NET 4, Silverlight RIA domain service

余生颓废 提交于 2019-12-13 02:37:38
问题 I have a small custom object defined as: public class TimeSeriesDefinition { public int classID; public DateTime startTime; public DateTime endTime; } I'm passing a List classIDs, a List startTimes, and a List endTimes into an RIA Domain Service function. As a matter of organization, I was grouping these values into a List of TimeSeriesDefinitions and then trying to use a foreach loop to create an expression that would select with AND operators between the values in a class and OR operators

DbContext get IQueryable for scalar system functions (Count, Any, Sum, Max)

久未见 提交于 2019-12-13 02:23:57
问题 I have DBContext with DbSet called Assignments. It's not a problem to create queryable for enumerable expressions and concatenated them, however I don't see the way to get IQueryable with deferred execution for functions like Count, Any, Max, Sum. Basically I want to have some IQueryable extension so I can execute it like this: IQueryable<int> query = myDbContext.SelectValue((ctx)=>ctx.Assignments.Where(...).Count()) .UnionAll(myDbContext.SelectValue((ctx)=>ctx.Assignments.Where(...).Count())

Table aggregation using linq (calculate Average value)

落爺英雄遲暮 提交于 2019-12-13 01:43:33
问题 How to Aggregate below table using linq query Date tagname value 06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(4) 54.73497 06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(5) 3.417564 06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(4) 94.82829 06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(4) 15.08091 06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(5) 3.6422 06-06-2018 14:15:00 Poll.Registers Block 0.310-PT-304_(4) 5.078211 06-06-2018 14:15:00 Poll

LINQ to EF Working with Lookup Tables

坚强是说给别人听的谎言 提交于 2019-12-13 01:35:09
问题 I have a main VendorProfile table and a 1-many VendorHistory table that contains status codes and date stamps. The query below works at retrieving only the latest status (status code and date) for each vendor. However, what I also want to do is translate the status code into the status name, which is located in StatusCodes lookup table. Model Diagram public IEnumerable<BrowseStatusModel> BrowseByStatus() { IQueryable<BrowseStatusModel> viewModel = _db.VendorProfiles .Include(

Linq to Entities - Subquery in the where statement

倖福魔咒の 提交于 2019-12-13 00:45:44
问题 This must be simple, but I've been searching for 2 hours, and can't find an answer. How do I write this in Linq to Entities: SELECT Reg4, Reg5, Reg6 FROM dbo.Table1 WHERE Reg1 = 15 AND Reg2 = ( SELECT MAX(Reg2) FROM dbo.Table2 WHERE Reg1 = 15); Is it possible to do it both in query expressions and method based syntaxes? Tks 回答1: var r1 = 15; var q = from t in Context.Table1 where t.Reg1 == r1 && t.Reg2 == Context.Table2 .Where(t2 => t2.Reg1 == r1) .Max(t2 => t2.Reg2) select t; Easier still if

Linq: Dynamic Query Contruction: query moves to client-side

白昼怎懂夜的黑 提交于 2019-12-13 00:37:27
问题 I've been following with great interest the converstaion here: Construct Query with Linq rather than SQL strings with regards to constructing expression trees where even the table name is dynamic. Toward that end, I've created a Extension method, addWhere, that looks like: static public IQueryable<TResult> addWhere<TResult>(this IQueryable<TResult> query, string columnName, string value) { var providerType = query.Provider.GetType(); // Find the specific type parameter (the T in IQueryable<T>

How can I dynamically customize a POCO proxy in EF 4?

╄→гoц情女王★ 提交于 2019-12-13 00:28:33
问题 I would like to dynamically customize some POCO classes overriding myself the virtual members to be able to compile LINQ to Entities queries. I know about the ObjectMaterialized event but that happens after the class instantiation. I would like to be able to create the proxy myself, override the virtual members I want and then pass along to the EF, is that possible? Imagine the following POCO class: public class Consumer { /* That´s a virtual property with an association in EF */ public