linq-to-entities

How to create SQL Nested ANDs inside of multiple ORs using LINQ

萝らか妹 提交于 2019-12-12 12:20:52
问题 I'm trying to create the equivelant LINQ query from the below SQL example: SELECT * FROM FOO WHERE ((a == <val1>) AND (b == <val2>) AND (c == <val3>)) OR ((a == <val4>) AND (b == <val5>) AND (c == <val6>)) There will always be an a, b, and c that will be AND'd together surrounded by an OR. This pattern can occur n amount of times. The only solution I have found that work is using LINQ Union but the SQL generated isn't what I would like. 回答1: Try implementing the PredicateBuilder class. Then

Deploying applications that use LINQ to Entities

纵然是瞬间 提交于 2019-12-12 11:22:25
问题 I want to use L2E since it's very convenient to my company's apps, I created a demo project, the demo does run on every machine but when I, let's say, press a button that has some code that uses the entity I get this error: specified store provider cannot be found in the configuration, or is not valid. note that I get this error only on machines that does not have VS2008 installed, on these machines (the ones with VS2008) the demo works well. any advice is appreciated. I am using MySql server

Get only one (last) record in one-to-many join with linq-to-entities

随声附和 提交于 2019-12-12 10:54:57
问题 I have the following in linq-to-entities clientprojects = (from p in this.SAPMappingEntities.SAP_Master_Projects join c in this.SAPMappingEntities.SAP_Master_ProjectPartners on c.project_no equals p.project_no where c.partner_name.Contains(clientstring) orderby p.start descending select new ClientProjects { client = c.partner_name, location = c.city +", "+c.region, project_no = c.project_no, start_dt = p.start, end_dt = p.finish }).Take(50).ToList(); I would like change this query so that for

Get sub collection in List<class> form

社会主义新天地 提交于 2019-12-12 10:45:46
问题 I have a LINQ to EF query that returns data in a class form. The class has a List<RecipeCategories> property that I need to populate. The RecipeCategories table is a relationship table between the Recipe table and the RecipeCategories table, and can be many to many. I found enough information to get the code to compile, but it errors at runtime and I haven't been able to figure out how to get this right. ri = (from r in recipeData.Recipes where r.ID == recipeId select new RecipeItem { Id = r

How to define expression selection nested one-to-one relationship in Entity Framework

孤街醉人 提交于 2019-12-12 10:17:41
问题 I am trying to run a Linq-to-Entity that contains this Expression into Entity Framework. Not Working: //2 seperated expressions, 1st expression calling the 2nd expression public Expression<Func<User, UserDto>> UserToDtoExpr() { var expr = AddressToDtoExpr(); return x => new UserDto() { Id = x.Id, Name = x.Name, Address = expr.Compile()(x.Address) }; } public Expression<Func<Address, AddressDto>> AddressToDtoExpr() { return x => New AddressDto() { Id = x.Id, City = x.City, Country= x.Country }

Entity Framework 4 - How to cap the number of child elements from another table (connected via a foreign key)

我怕爱的太早我们不能终老 提交于 2019-12-12 09:53:13
问题 I have two data models Blog and Post . BlogId is a foreign key on the Post table public class Blog { public int ID { get; set; } public string Title { get; set; } public virtual ICollection<Post> Posts { get; set; } ... } public class Post { public int ID { get; set; } public virtual int BlogId { get; set; } public string Title { get; set; } ... } Now this works fine and my Repository is happy and pulls everything as expected from DB. My question is - Is there a way to limit the number of

How do I add a navigation property for a Entity Framework Complex Type

房东的猫 提交于 2019-12-12 08:54:24
问题 I'm using VS2010 Beta 2, I have a Complex Type called Address with the following properties: Street City CountryId I have a Country Entity defined in my Model, but I can't seem to find a way to add a reference (Navigation Property) from the CountryId property of my Complex Type to the Id property of my Country entity. I'm I going about this the wrong way or is this something that I can't do with the designer??... Another option I have is just creating an Address entity, but it just doesn't

The right way to insert multiple records to a table using LINQ to Entities

纵然是瞬间 提交于 2019-12-12 08:17:27
问题 As many of us have done, I set up a simple loop to add multiple records from a databse. A prototypical example would be something like this: Method I: // A list of product prices List<int> prices = new List<int> { 1, 2, 3 }; NorthwindEntities NWEntities = new NorthwindEntities(); foreach (int price in prices) { Product newProduct = new Product(); newProduct.Price = price; NWEntities.Products.AddObject(newProduct); } NWEntities.SaveChanges(); When I first set up the loop, however, I

Is it possible to get Entity Framework to recognize objects that have been created and not yet saved in database?

独自空忆成欢 提交于 2019-12-12 08:14:33
问题 Say Table1 is a table with two columns. Table1ID and Name. If I do the following code... var Obj1 = new Table1(); Obj1.Name = "hello" TestDBEntities.AddToTable1(Obj1); var currObj = Table1.Where(o => o.Name.Contains("hello")).FirstOrDefault(); currObj will return null. But if I do this var Obj1 = new Table1(); Obj1.Name = "hello" TestDBEntities.AddToTable1(Obj1); **TestDBEntitles.SaveChanges();** var currObj = Table1.Where(o => o.Name.Contains("hello")).FirstOrDefault(); Then currObj will

How to find a date with a list of specified date ranges in Entity Framework?

寵の児 提交于 2019-12-12 07:24:55
问题 I have an IEnumerable of a class that I created to contain date ranges. That class looks like this: public class Range<T> where T: struct { public T Start { get; set; } public T End { get; set; } } I want to find all the records in my set where a date column falls within ANY of the specified date ranges. This was my attempt: deals = deals.Where( deal => criteria.DateRanges.Any( dt => deal.CloseDate >= dt.Start && deal.CloseDate < dt.End.Value.AddDays(1))); This throws an error I assume