linq

get a List of Max values across a list of lists

我是研究僧i 提交于 2021-02-07 20:29:46
问题 I have a List<List<double>> and I need to find a List MyList where MyList[0], for instance, is the Max of all the first elements of the List. Example, just to be clear: First list contains (3,5,1), second contains (5,1,8), third contains (3,3,3), fourt contains (2,0,4). I need to find a list with (5, 5, 8). I do NOT need the list (5,8,3,4). Of course i know how to do it with nested for cycles. I'd like to know if there's a linq way and believe me i don't know where to start from. 回答1: Try

Creating a lambda expression at runtime

让人想犯罪 __ 提交于 2021-02-07 18:49:46
问题 I have a repository class which has a GetAsQueryable method defined as follows: public class Repository<TEntity> : IDisposable, IRepository<TEntity> where TEntity : class { internal DbSet<TEntity> _DbSet; public virtual IQueryable<TEntity> GetAsQueryable( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "") { IQueryable<TEntity> query = _DbSet; if (filter != null) { query = query.Where(filter); }

Creating a lambda expression at runtime

痞子三分冷 提交于 2021-02-07 18:49:10
问题 I have a repository class which has a GetAsQueryable method defined as follows: public class Repository<TEntity> : IDisposable, IRepository<TEntity> where TEntity : class { internal DbSet<TEntity> _DbSet; public virtual IQueryable<TEntity> GetAsQueryable( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "") { IQueryable<TEntity> query = _DbSet; if (filter != null) { query = query.Where(filter); }

LINQ: The cast to value type 'System.Int32' failed because the materialized value is null

微笑、不失礼 提交于 2021-02-07 14:25:59
问题 I'm getting the following error when executing a LINQ query on my database: The cast to value type 'System.Int32' failed because the materialized value is null. I believe it's because one of the columns is returning a null. Here is my LINQ command: var facts = (from b in Program.db.ProductBrands join bm in Program.db.BrandManufacturers on b.ProductBrandID equals bm.ProductBrandID join c in Program.db.Companies on bm.ProductManufacturerID equals c.CompanyID join s in Program.db.AnimalSources

LINQ: The cast to value type 'System.Int32' failed because the materialized value is null

淺唱寂寞╮ 提交于 2021-02-07 14:24:20
问题 I'm getting the following error when executing a LINQ query on my database: The cast to value type 'System.Int32' failed because the materialized value is null. I believe it's because one of the columns is returning a null. Here is my LINQ command: var facts = (from b in Program.db.ProductBrands join bm in Program.db.BrandManufacturers on b.ProductBrandID equals bm.ProductBrandID join c in Program.db.Companies on bm.ProductManufacturerID equals c.CompanyID join s in Program.db.AnimalSources

Entity Framework ToListAsync() with Select()

 ̄綄美尐妖づ 提交于 2021-02-07 13:46:58
问题 I have the following ActionResult in my controller public async Task<ActionResult> NewTickets() { // Show tickets for all divisions a agent is in var user = "abcdefg"; var company = "company1"; var tickets = (from a in db2.Ticket join c in db2.Division on a.DivisionId equals c.DivisionId join dp in db2.DivisionParticipator on c.DivisionId equals dp.DivisionId where c.CompanyId == company.CompanyId && a.Status == "New" && dp.ApplicationUserId == user.Id select new { Id = a.Id, DivisionId = a

How to check for null before I use in linq?

廉价感情. 提交于 2021-02-07 11:15:32
问题 I have an list of objects that contains another object in it. List<MyClass> myClass = new List<MyClass>(); I want to do some linq like this myClass.Where(x => x.MyOtherObject.Name = "Name").ToList(); Thing is sometimes "MyOtherObject" is null. How do I check for this? 回答1: Simple, just add an AND clause to check if it's not null: myClass.Where(x => x.MyOtherObject != null && x.MyOtherObject.Name = "Name").ToList(); 回答2: As of C# 6, you can also use a null conditional operator ?. : myClass

C# Select query not modifying external variable

梦想与她 提交于 2021-02-07 09:34:51
问题 I have a class with n elements, and a property that returns the root sum square of the elements: public double Length { get { double sum = 0.0; Elements.Select(t => sum += t * t); return Math.Sqrt(sum); } } However, that doesn't work - no matter the value of the elements, sum remains 0.0. Why doesn't this work? Note: I have already implemented it another way, but am looking to understand why the above code does not work 回答1: LINQ uses deferred execution – the Select Method doesn't execute the

C# Select query not modifying external variable

白昼怎懂夜的黑 提交于 2021-02-07 09:34:21
问题 I have a class with n elements, and a property that returns the root sum square of the elements: public double Length { get { double sum = 0.0; Elements.Select(t => sum += t * t); return Math.Sqrt(sum); } } However, that doesn't work - no matter the value of the elements, sum remains 0.0. Why doesn't this work? Note: I have already implemented it another way, but am looking to understand why the above code does not work 回答1: LINQ uses deferred execution – the Select Method doesn't execute the

Does Linq's IEnumerable.Select return a reference to the original IEnumerable?

醉酒当歌 提交于 2021-02-07 09:26:56
问题 I was trying to clone an List in my code, because I needed to output that List to some other code, but the original reference was going to be cleared later on. So I had the idea of using the Select extension method to create a new reference to an IEnumerable of the same elements, for example: List<int> ogList = new List<int> {1, 2, 3}; IEnumerable<int> enumerable = ogList.Select(s => s); Now after doing ogList.Clear() , I was surprised to see that my new enumerable was also empty. So I