linq-to-entities

Union in entity framework

狂风中的少年 提交于 2019-12-12 07:07:16
问题 I have two tables: Vehicles and Workers. Vehicle(Id, Number) Workers(Id, Name, ContractorVehicleNumber) I would like to write lambda query to return all the vehicles and the contractor vehicles. Something like in sql: SELECT Id, Number FROM Vehicle UNION SELECT NULL, ContractorVehicleNumber FROM Workers This is what I made: public IQueryable<Vehicle> Get(bool includeContractorVehicles) { IQueryable<Vehicle> query = GetQuery(); if (includeContractorVehicles == true) { WorkerRepository rep =

Compare dd-mm-yyyy datetime

回眸只為那壹抹淺笑 提交于 2019-12-12 06:47:14
问题 Problem I want to compare "dd-mm-yyyy" DateTime's without factoring in the time. Attempt I have tried comparing the standard DateTime values with my database DateTime's like this: C#/LINQ: var startDate = DateTime.Today.AddDays(-10); var endDate = DateTime.Today; dateList= (from x in db.MY_DB where (x.DATE >= startDate && x.DATE < endDate) select x).ToList(); However, my list never gets populated, even though many entries meet this criteria, which I verified with the following query in SQL:

How to workaround “Contains” for .net 3.5 don't support

纵饮孤独 提交于 2019-12-12 05:47:57
问题 I write a linq to Entity: string[] groups = GetGroups(); var fList = from f in _store.wcf_ServerFarm join a in _store.ClientAccess on f.ServerFarmName equals a.AccessServerFarmName join s in _store.Service on f.ServerFarmName equals s.ServerFarmName where groups.Contains(s.ServerMachineName) select new { f.ServerFarmAddress, s.ServerMachineName, s.ServiceName, s.ServiceConfig, s.ServicePath }; But .net 3.5 didn't support Contains(), I couldn't upgrade it for some reason. how to figure out it.

Linq Method Syntax - Can not convert IQueryable to Bool

随声附和 提交于 2019-12-12 05:36:18
问题 I try to re-factor query 1 to query 2 syntax... because of more more readable etc. paramaters like "group, begindate, enddate and excludeManifestatie are passed via argument (method parms).. I check whether they are blank or not )passed or not so I can built my dynamic Linq SQL. But I get error in Query 1 when I use ....HasValue check. It says: "Cannot implicitly convert type 'System.Linq.IQueryable' to 'bool'" Query 2 works fine and that's what I want, but it has complicated syntax, so I

Convert SQL to LINQ to Entities WHERE IN clause

怎甘沉沦 提交于 2019-12-12 04:59:50
问题 How can I convert this sql statetment to LINQ to Entities? SQL Statement: Select * from Departments where DepartmentID in (Select DepartmentID from Employees where FirstName like '%FirstName%' or LastName like '%LastName%') All I have is WHERE =(equals) , I can't do the WHERE IN clause LINQ to Entities: from t in db.Departments where t.DepartmentID == -->Should be IN not EQUALS ((from t0 in db.Employees where t0.FirstName.Contains("FirstName") || t0.LastName.Contains("LastName") select new {

How get array in linq to entity?

喜欢而已 提交于 2019-12-12 04:59:37
问题 var sites = from country in db.Countries select new SitiesViewByUser() { Country = country.Title, City = country.Cities .Select(m => m.Title).ToArray() }; City - array string. I need get array Cities.Title this code: foreach(var item in sites) get this error: Expression of LINQ to Entities does not recognize the method "System.String [] ToArray [String] (System.Collections.Generic.IEnumerable` 1 [System.String]) ", so it can not be converted into an expression store. 回答1: You should be able

Entity Framework inline SQL dynamically select table name

社会主义新天地 提交于 2019-12-12 04:56:07
问题 I have a less than optimal database that I am querying which strangely, instead of storing look up items in a parent child relationship, stores them in individual tables. So, for example, there is a table for countries and one for states etc. I cannot change the database structure, keeping this in mind, I would like to create a method where I simply pass in table name, and a field name, and I get a list of strings that I can use to load a dropdown. (I am using Entity Framework 6.x) However I

How to create a Linq To Entities expression

不羁岁月 提交于 2019-12-12 04:48:30
问题 HI, I'm using Linq To Entities and I'd like to convert this return db.Products .Where(p => p.idUser.Equals(id) && p.Category.Genre.Any(g => g.visible)) into something like Func<Genre, bool> expr = g => g.visible return db.Products .Where(p => p.idUser.Equals(id) && p.Category.Genre.Any(expr)) so I can add more complexity with something like this Func<Genre, bool> expr = g => g.visible expr += g => g.position < 5 But I always have an 'internal 1025 error .NET'. Can anyone help me, please?

how to filter nested list using Linq lambda

ぃ、小莉子 提交于 2019-12-12 04:43:13
问题 I have a class person which has a list of addresses and phones as the follow code. In my query I want a list of person but not including the address and phone that are already deleted, but is always returning all addresses and phones even they flag as deleted. How could I filter those nested lists using lambda? public class Person{ public int PersonId { get; set; } public string Name { get; set; } public virtual ICollection<Address> Addresses { get; set; } public virtual ICollection<Phone>

Maximizing Performance with the Entity Framework [duplicate]

戏子无情 提交于 2019-12-12 04:17:47
问题 This question already has answers here : How to “warm-up” Entity Framework? When does it get “cold”? (5 answers) Closed 3 years ago . I am developing travel web site. When user input a location for search(autocomplete) my action return all cities, that cities regions, regions, regions translations, hotels..... which start with user input I used Entity code first. But it is response time is too much. How can I optimize this? How can I decrease time? public JsonResult AutoComplateCityxxxxxxxx