linq-to-entities

LINQ Query - how sort and filter on eager fetch

白昼怎懂夜的黑 提交于 2019-12-18 07:17:09
问题 How do I do a eager query of a parent child relationship that: filters a on child fields sorts on both parent and child return a List or Parents with the children pre-populated If I try from p in _context.Parents.Include("children") join c in _context.childrenon p.Id equals c.ParentId where d.DeletedDate == null orderby p.Name ascending, c.Name select p Then I get the Parent object back but each Parent has NULL for children if I try from p in _context.Parents.Include("children") orderby p

Linq Entity Framework generic filter method

ⅰ亾dé卋堺 提交于 2019-12-18 07:03:35
问题 I have some methods which perform a standard filter on data from my Entities (using Entity Framework v4). Example #1: protected IQueryable<Database.Product> GetActiveProducts( ObjectSet<Database.Product> products ) { var allowedStates = new string[] { "Active" , "Pending" }; return ( from product in products where allowedStates.Contains( product.State ) && product.Hidden == "No" select product ); } Example #2: protected IQueryable<Database.Customer> GetActiveProducts( ObjectSet<Database

How to Type Cast dynamically to string while using Contains in Dynamic LINQ?

人走茶凉 提交于 2019-12-18 05:15:28
问题 I want to use Dynamic LINQ Query to Search with some text in all Properties in a class. i am using following function to create expression. I am passing property name and search text to the method. In that method if the property type is String then it is working fine. if the property type is int, DateTime, GUID. then it is not working. As we know Contains method only for array of elements or for string. I am thinking the value to property should type cast to string. So How to do it? Solution

Why does LINQ-to-Entites recognize my custom method?

只谈情不闲聊 提交于 2019-12-18 04:44:19
问题 This works: Entities.WorkOrderSet.Where(MyCustomMethod); This does not: Entities.WorkOrderSet.Where(o => MyCustomMethod(o)); ( [Edit] Even without new , it doesn't work) I understand why the second doesn't work - but why in the world does the first work!? Shouldn't I get a "LINQ-to-Entities does not recognize the method..." at runtime, like with the second? For reference, here is MyCustomMethod public bool MyCustomMethod(WorkOrder workOrder) { return !workOrder.WorkOrderNum.StartsWith("A",

MERGE in Entity Framework

折月煮酒 提交于 2019-12-18 04:44:12
问题 Is there a way to call T-Sql's MERGE command from .NET Entity framework 4? 回答1: No there no such built-in functionality - you must build your own. Very common is for example approach like: public void SaveOrUpdate(MyEntity entity) { if (entity.Id == 0) { context.MyEntities.AddObject(entity); } else { context.MyEntities.Attach(entity); context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); } // You can call SaveChanges here or you can call it separately after multiple

Get distinct records using linq to entity

…衆ロ難τιáo~ 提交于 2019-12-18 03:55:06
问题 Hi I'm using linq to entity in my application. I need to get distinct records based on one column value "Name" So I have a table similar like you can see below: (User) ID Name Country DateCreated I need to select all this items but uniques based on Name (unique). Is it possible to accomplish using linq, if so please show me how. var items = (from i in user select new {i.id, i.name, i.country, i.datecreated}).Distinct(); 回答1: The Distinct() method doesn't perform well because it doesn't send

How to do a “where in values” in LINQ-to-Entities 3.5

﹥>﹥吖頭↗ 提交于 2019-12-18 03:05:28
问题 Does anybody know how to apply a "where in values" type condition using LINQ-to-Entities? I've tried the following but it doesn't work: var values = new[] { "String1", "String2" }; // some string values var foo = model.entitySet.Where(e => values.Contains(e.Name)); I believe this works in LINQ-to-SQL though? Any thoughts? 回答1: It is somewhat of a shame that Contains is not supported in Linq to Entities. IN and JOIN are not the same operator (Filtering by IN never changes the cardinality of

Querying Entity with LINQ using Dyanmic Field Name

妖精的绣舞 提交于 2019-12-18 03:00:27
问题 I have created a dynamic search screen in ASP.NET MVC. I retrieved the field names from the entity through reflection so that I could allow the user to choose which fields they wanted to search on instead of displaying all fields in the view. When the search result is Posted back to the controller, I receive a FormCollection containing the FieldName and the Value. I don't know how many fields are being searched on, and the FormCollection only contains fields that were chosen by the user. I

LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method

扶醉桌前 提交于 2019-12-18 02:53:20
问题 I am trying to convert one application to EntityFrameWork codefirst. My present code is string sFrom ="26/12/2013"; select * FROM Trans where CONVERT(datetime, Date, 105) >= CONVERT(datetime,'" + sFrom + "',105) And i tried DateTime dtFrom = Convert.ToDateTime(sFrom ); TransRepository.Entities.Where(x =>Convert.ToDateTime(x.Date) >= dtFrom) But I got an error like this LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method Please help Thanks in

I cannot get the output parameter when use function import by Entity Framework

自古美人都是妖i 提交于 2019-12-17 23:35:20
问题 Here's my SQL Server stored procedure : ALTER PROCEDURE [dbo].[SearchUser] (@Text NVARCHAR(100), @TotalRows INT = 0 OUTPUT) AS BEGIN SELECT @TotalRows=1000 SELECT * from Users END And my C# code using (var context = new TestDBEntities()) { var outputParameter = new ObjectParameter("TotalRows", typeof(Int32)); context.SearchUser("", outputParameter); Response.Write(outputParameter.Value); } However outputParameter.Value always is null. Could anybody tell me why? 回答1: Output parameters filled