linq-to-entities

How to tell if an IEnumerable<T> is subject to deferred execution ?

左心房为你撑大大i 提交于 2020-01-09 06:17:28
问题 I always assumed that if I was using Select(x=> ...) in the context of LINQ to objects, then the new collection would be immediately created and remain static. I'm not quite sure WHY I assumed this, and its a very bad assumption but I did. I often use .ToList() elsewhere, but often not in this case. This code demonstrates that even a simple 'Select' is subject to deferred execution : var random = new Random(); var animals = new[] { "cat", "dog", "mouse" }; var randomNumberOfAnimals = animals

Convert SQL Query to LINQ-to-SQL

一笑奈何 提交于 2020-01-07 05:28:07
问题 I need help converting SQL query to LINQ to SQL select top 5 customer_id, customer_name, product_id from Customer Join Product on product_id = product_id where (customer_active = 'TRUE') order by checksum(newid()) How can I do that in LINQ to SQL. Thanks 回答1: This was solved. Thanks to 'CodeNotFound' for this answer https://stackoverflow.com/a/43850748/1655774 db.Customer.Where(p => p.customer_active == true).Select(p => new CustomerViewModel { Customer_id= p.customer_id, Customer_name = p

Help With Generic LINQ OrderBy Lambda Expression

北城余情 提交于 2020-01-06 09:33:11
问题 Trying to get an OrderBy on an IQueryable to work, not having much luck. Here's my method: public ICollection<T> FindAll<T>(Expression<Func<T,bool>> predicate, Expression<Func<T,int>> orderingKey) where T : Post { return repository .Find() .Where(predicate) .OfType<T>() .OrderBy(orderingKey) .ToPagedList(); } The ordering works if i do this: FindAll(p => p.PostName == "Foo", p => p.PostId); But not if i want to do this: FindAll(p => p.PostName == "Foo", p => p.DateModified); It doesn't work

Help With Generic LINQ OrderBy Lambda Expression

我是研究僧i 提交于 2020-01-06 09:31:28
问题 Trying to get an OrderBy on an IQueryable to work, not having much luck. Here's my method: public ICollection<T> FindAll<T>(Expression<Func<T,bool>> predicate, Expression<Func<T,int>> orderingKey) where T : Post { return repository .Find() .Where(predicate) .OfType<T>() .OrderBy(orderingKey) .ToPagedList(); } The ordering works if i do this: FindAll(p => p.PostName == "Foo", p => p.PostId); But not if i want to do this: FindAll(p => p.PostName == "Foo", p => p.DateModified); It doesn't work

How to select an object through a foreign key

…衆ロ難τιáo~ 提交于 2020-01-06 08:13:34
问题 If I have a table with a primary key (AccLinkID) and a foreign key (aspnet_Users UserID), how can I select the object that the foreign key points to using Linq to Entities. User myUser = _myDB.AccLinkSet.Where(user => user.LinkID == linkId).FirstOrDefault().aspnet_Users; did not work... anyone have any ideas? 回答1: Try this: User myUser = _myDB.AccLinkSet.Include("aspnet_Users") .Where(user => user.LinkID == linkId).FirstOrDefault().aspnet_Users; 回答2: Although you can solve this with Include,

Query objects for one to many relationship in LINQ

自作多情 提交于 2020-01-06 07:54:35
问题 I have a problem which seems to be quite similar to Query objects for one to many relationship in LINQ I've been trying to solve my problem referring the above but somehow I've been failing for a long time. The scenario I have a model named Business which has a ICollection of Branches and also the business belongs to a particular SubCategory . The model Looks something like public class SubCategory { public string Id { get; set; } public string Name { get; set; } public string Description {

Query objects for one to many relationship in LINQ

Deadly 提交于 2020-01-06 07:54:11
问题 I have a problem which seems to be quite similar to Query objects for one to many relationship in LINQ I've been trying to solve my problem referring the above but somehow I've been failing for a long time. The scenario I have a model named Business which has a ICollection of Branches and also the business belongs to a particular SubCategory . The model Looks something like public class SubCategory { public string Id { get; set; } public string Name { get; set; } public string Description {

Aggregate table using linq to entities

心不动则不痛 提交于 2020-01-06 06:48:24
问题 I've a table like this Id MachineName ServerName TagName TagValue ValueDateTime 1 DanPac_A Daniel.Device Links.1 Poll.Registers Block 0.STR02_METER_DENSITY_(1) 93.1631400000 2018-06-04 21:08:28.720 2 DanPac_A Daniel.Device Links.1 Diagnostics.Heartbeat 1.0000000000 2018-06-04 21:08:32.717 3 DanPac_A Daniel.Device Links.1 Poll.Registers Block 0.310-TT-302_(6) 52.1062000000 2018-06-04 21:08:32.873 4 DanPac_A Daniel.Device Links.1 Poll.Registers Block 0.310-TT-302_(6) 52.1062000000 2018-06-04 21

Aggregate table using linq to entities

允我心安 提交于 2020-01-06 06:48:18
问题 I've a table like this Id MachineName ServerName TagName TagValue ValueDateTime 1 DanPac_A Daniel.Device Links.1 Poll.Registers Block 0.STR02_METER_DENSITY_(1) 93.1631400000 2018-06-04 21:08:28.720 2 DanPac_A Daniel.Device Links.1 Diagnostics.Heartbeat 1.0000000000 2018-06-04 21:08:32.717 3 DanPac_A Daniel.Device Links.1 Poll.Registers Block 0.310-TT-302_(6) 52.1062000000 2018-06-04 21:08:32.873 4 DanPac_A Daniel.Device Links.1 Poll.Registers Block 0.310-TT-302_(6) 52.1062000000 2018-06-04 21

EntityFramework LINQ Order using string and nested reflection

怎甘沉沦 提交于 2020-01-06 06:30:37
问题 I saw already some of the similar questions, but cannot find an anwser how to solve this problem. I want to have a possibility to order collection using string as property name. Model: public sealed class User : IdentityUser { #region Constructors #endregion #region Properties [Required] [Display(Name = "First name")] public string FirstName { get; set; } [Required] [Display(Name = "Last name")] public string LastName { get; set; } [ForeignKey("Superior")] public string SuperiorId { get; set;