linq-to-entities

Include and Where predicate cause left join instead of inner join

别来无恙 提交于 2020-01-13 08:43:08
问题 With the following table structure (extraneous columns removed) create table [Events] ( ID int not null identity, Name nvarchar(128) not null, constraint PK_Events primary key(ID) ) create table [Donations] ( ID int not null identity, EventID int not null, Amount decimal(10, 2) not null, constraint PK_Donations primary key(ID), constraint FK_Donations_Events foreign key(EventID) references [Events](ID) on update no action on delete no action ) I use the following Linq-to-Entities queries: //

LINQ to Entities select columns with expression parameter

帅比萌擦擦* 提交于 2020-01-13 05:18:06
问题 I cannot wrap my head around how i manage to select columns in a queryable by specifying an expression as a parameter. Method A(IQueryable<Order> query) Inside Method A i want to specify which columns to select, so i dont get all columns right away, like this: query.Select(x => new { x.OrderNumber, x.Payment, x.Customer }) This is easy if i specify this directly in Method A, but i want to pass the information using a parameter. I tried using a expression like this: Expression<Func<Order,

Modifying dates in ASP.NET MVC C# based on stored time zone

孤人 提交于 2020-01-13 05:16:30
问题 My question is two-fold. 1) I am coding a forum and I'm having trouble figuring out how to store time zones for the users of the forum. They will be able to set their time zone and have all dates on the forum modified accordingly. Do I have to create a DB table with timezone names and the number to adjust the server time by? Does .NET have time zone support built in somewhere? 2) Once I've figured out how to store the user's time zone and then modify a DateTime object to the right time, I

Entity Framework - Include in sub query?

落爺英雄遲暮 提交于 2020-01-13 04:29:38
问题 I'm not sure if this has been answered yet, I looked at a couple of questions but I don't think they were quite what I was after. Let's say I have 3 tables: Restaurant 1.....M MenuCategory 1.....M MenuItem I have a L2E query that looks something like this: Restaurant = context.Restaurant .Include(r => r.MenuCategory) .FirstOrDefault(r => r.RestaurantId == resaurantId); Which works to some extent, but it only pre-loads the menu categories. As a work around I am able to iterate around each

Entity Framework - Include in sub query?

*爱你&永不变心* 提交于 2020-01-13 04:29:21
问题 I'm not sure if this has been answered yet, I looked at a couple of questions but I don't think they were quite what I was after. Let's say I have 3 tables: Restaurant 1.....M MenuCategory 1.....M MenuItem I have a L2E query that looks something like this: Restaurant = context.Restaurant .Include(r => r.MenuCategory) .FirstOrDefault(r => r.RestaurantId == resaurantId); Which works to some extent, but it only pre-loads the menu categories. As a work around I am able to iterate around each

How can I transform this linq expression?

牧云@^-^@ 提交于 2020-01-13 03:40:08
问题 Say I have an entity that I want to query with ranking applied: public class Person: Entity { public int Id { get; protected set; } public string Name { get; set; } public DateTime Birthday { get; set; } } In my query I have the following: Expression<Func<Person, object>> orderBy = x => x.Name; var dbContext = new MyDbContext(); var keyword = "term"; var startsWithResults = dbContext.People .Where(x => x.Name.StartsWith(keyword)) .Select(x => new { Rank = 1, Entity = x, }); var

Using an IEqualityComparer with a LINQ to Entities Except clause

偶尔善良 提交于 2020-01-13 02:41:11
问题 I have an entity that I'd like to compare with a subset and determine to select all except the subset. So, my query looks like this: Products.Except(ProductsToRemove(), new ProductComparer()) The ProductsToRemove() method returns a List<Product> after it performs a few tasks. So in it's simplest form it's the above. The ProductComparer() class looks like this: public class ProductComparer : IEqualityComparer<Product> { public bool Equals(Product a, Product b) { if (ReferenceEquals(a, b))

Using an IEqualityComparer with a LINQ to Entities Except clause

匆匆过客 提交于 2020-01-13 02:41:08
问题 I have an entity that I'd like to compare with a subset and determine to select all except the subset. So, my query looks like this: Products.Except(ProductsToRemove(), new ProductComparer()) The ProductsToRemove() method returns a List<Product> after it performs a few tasks. So in it's simplest form it's the above. The ProductComparer() class looks like this: public class ProductComparer : IEqualityComparer<Product> { public bool Equals(Product a, Product b) { if (ReferenceEquals(a, b))

Performance: .Join vs .Contains - Linq to Entities

柔情痞子 提交于 2020-01-12 15:25:54
问题 I am using Linq to entities to query the database to get the list of int for further processing. I have two ways to get the list as below: First is: List<int> lstBizIds = new List<int>() { 1, 2, 3, 4, 5 }; List<int> lstProjectIds = context.Projects.Where(x => lstBizIds.Contains(x.businessId)).Select(x => x.projectId).ToList(); Second is: List<int> lstBizIds = new List<int>() { 1, 2, 3, 4, 5 }; List<int> lstProjectIds = context.Projects.Join(lstBizIds, p => p.businessId, u => u, (p, u) => p

How to stay DRY whilst using LINQ to Entities and helper methods?

一笑奈何 提交于 2020-01-12 15:19:10
问题 Lets say that I have a particular way of deciding whether some strings "match", like this: public bool stringsMatch(string searchFor, string searchIn) { if (string.IsNullOrEmpty(searchFor)) { return true; } return searchIn != null && (searchIn.Trim().ToLower().StartsWith(searchFor.Trim().ToLower()) || searchIn.Contains(" " + searchFor)); } I would like to pull matches out of a database using Linq To Entities and this helper. However, when I try this: IQueryable<Blah> blahs = query.Where(b =>