linq-to-entities

asp.net mvc and linq to entities: how to include text value from IEnumerable<SelectListItem>

牧云@^-^@ 提交于 2020-01-01 18:14:40
问题 I have a table with a constraint on one field - it can be 1, 2 or 3. (The correct solution to this is probably to create a lookup table for this, but for now I'm wondering if it's possible to do this without the lookup table.) I've created a class that returns an IEnumerable for the values. I'm using LINQ to Entities, and would like to be able to display the text value in a col. when listing all the entities. The code for create/edit looks like: Controller.cs: ViewData["Message_Types"] =

asp.net mvc and linq to entities: how to include text value from IEnumerable<SelectListItem>

橙三吉。 提交于 2020-01-01 18:13:30
问题 I have a table with a constraint on one field - it can be 1, 2 or 3. (The correct solution to this is probably to create a lookup table for this, but for now I'm wondering if it's possible to do this without the lookup table.) I've created a class that returns an IEnumerable for the values. I'm using LINQ to Entities, and would like to be able to display the text value in a col. when listing all the entities. The code for create/edit looks like: Controller.cs: ViewData["Message_Types"] =

On local PC IIS and Browser maxes out CPU with large result set

爱⌒轻易说出口 提交于 2020-01-01 12:13:38
问题 I just upgraded to VS 2013 and also started a new MVC project, and am being faced with 99% CPU usage for a couple minutes by IIS while debugging on my local PC. (Happens in both Release and Debug mode) I just realized that the problem is proportional to the number of lines returned by by Linq Query. If I use Take(1) or Take(10) it's fine. If I use Take(100) the problem occurs. Here is my actionresult (with private info altered): public ActionResult Summary(string daystoshow, DateTime? day =

Linq Paging - How to incorporate total record count

无人久伴 提交于 2020-01-01 10:12:25
问题 I am trying to figure out the best way of getting the record count will incorporating paging. I need this value to figure out the total page count given a page size and a few other variables. This is what i have so far which takes in the starting row and the page size using the skip and take statements. promotionInfo = (from p in matches orderby p.PROMOTION_NM descending select p).Skip(startRow).Take(pageSize).ToList(); I know i could run another query, but figured there may be another way of

LINQ to Entities — OrderBy().ToLisT() vs. ToList().OrderBy()

心不动则不痛 提交于 2020-01-01 09:33:30
问题 I'm looking for confirmation/clarification with these LINQ expressions: var context = new SomeCustomDbContext() // LINQ to Entities? var items = context.CustomItems.OrderBy(i => i.Property).ToList(); // LINQ to Objects? var items2 = context.CustomItems.ToList().OrderBy(i => i.Property); (Q1) Am I correct in thinking the first method is LINQ to Entities where EF builds a more specific SQL statement to pass on, putting the ordering effort on on the database? (Q2) Is the second method LINQ to

What is the best way to cast each item in a LINQ to Entities query to an interface?

两盒软妹~` 提交于 2020-01-01 09:23:22
问题 I have an entity object 'User' which implements 'IUser': IQueryable<User> users = Db.User; return users; But what I actually want to return is: IQueryable<IUser> So what is the best way to convert IQueryable<User> to IQueryable<IUser> without actually running the query? Right now I am doing this but it seems like a hack: IQueryable<IUser> users = Db.User.Select<User, IUser>(u => u); 回答1: Your "hacky" solution looks fine to me. 来源: https://stackoverflow.com/questions/1240087/what-is-the-best

How to use LINQ to select into an object?

本秂侑毒 提交于 2020-01-01 07:28:06
问题 I have data that looks like so: UserId | SongId -------- -------- 1 1 1 4 1 12 2 95 I also have the following class: class SongsForUser { public int User; public List<int> Songs; } What I would like to do is use LINQ to select from my data to create a collection of SongsForUser objects. Below is what I have come up with so far: var userCombos = songs.UserSongs.Select(x => new SongsForUser() { User = x.UserId, Songs = /*What goes here?*/ }); How would I go about populating my Songs List? So

Linq to Entities SqlFunctions.DateDiff not supported

女生的网名这么多〃 提交于 2020-01-01 05:33:07
问题 I have the following code DateTime now = DateTime.UtcNow; var allItemsOver64 = _inventoryContext.Items.Where(i => (SqlFunctions.DateDiff("dd", i.PrimaryInsured.DoB, now) / 365.0) >= 65); IQueryable<Item> items65To69 = allItemsOver64.Where(i => (SqlFunctions.DateDiff("dd", i.PrimaryInsured.DoB, now) / 365.0) >= 65 && (SqlFunctions.DateDiff("dd", i.PrimaryInsured.DoB, now) / 365.0) <= 69); But when I try and use allItemsOver64 thus Items65To69.Count() I get this error The expression ((((Convert

How can I compose an Entity Framework query from smaller, resusable queries?

删除回忆录丶 提交于 2020-01-01 05:28:45
问题 I have a few (fairly redundant) queries in my app that are something like this: var last30Days = DateTime.Today.AddDays(-30); from b in Building let issueSeverity = (from u in Users where u.Building == b from i in u.Issues where i.Date > last30Days select i.Severity).Max() select new { Building = b, IssueSeverity = issueSeverity } And: var last30Days = DateTime.Today.AddDays(-30); from c in Countries let issueSeverity = (from u in Users where u.Building.Country == c from i in u.Issues where i

C# - LINQ - shortest distance by GPS latitude and longitude

[亡魂溺海] 提交于 2020-01-01 04:52:09
问题 I have database and in it I have class hotel with gps coordinates. I want to get closest places to coordinates which I choose. I think It should look like this (I found many example codes here and like this one): var coord = new GeoCoordinate(latitude, longitude); var nearest = (from h in db.hotels let geo = new GeoCoordinate(h.gps.lat, h.gps.lng) orderby geo.GetDistanceTo(coord) select h).Take(10); The problem is that I have this error when I tried to search for something: Only parameterless