linq

Summing a value inside of a Anonymous Type

Deadly 提交于 2021-02-08 12:03:57
问题 I have this in my code: var pRating = ctx.PrProjectRating .Where(x => x.PrIsDeleted == false) .Select(k => k) .GroupBy(g => new {g.PrPIdG}, (key, group) => new { sumR = group.Sum(k => k.PrValue), pidG = key.PrPIdG }); var pLike = ctx.PlProjectLike .Where(x => x.PlValue == "Like") .Select(c => c) .GroupBy(g => new {g.PlPIdG}, (key, group) => new { sumR = group.Count(), pidG = key.PlPIdG }) .OrderByDescending(g => g.sumR); var pConnect = ctx.PcProjectConnect .Where(x => x.PcStatus == "Connected

How to do the convertion to Expression tree of lambda .Where() after .join() using anonymous type?

自古美人都是妖i 提交于 2021-02-08 11:39:42
问题 How to finish the convertion of the following query to Expression tree syntax, using an anonymous type in the .Where() and .Select()? IQueryable<A> As = db.A .Join( db.B, _a => _a.bID, _b => _b.ID, (a, b) => new { a, b }) //next two are the objective .Where(s=> s.b.Name == "xpto") .Select(s => s.a); At this point I have (Thanks to @NetMage): //QUERY DB with GENERIC TYPE IQueryable<B> queryable = (IQueryable<B>)db.B; // IQueryable<TOuter> var arg0 = Expression.Constant(db.A.AsQueryable()); //

Combining elements in the same list

自作多情 提交于 2021-02-08 11:08:10
问题 suppose I have a list that comes from a database like this: List<Item> list = { new Item { TypeID = 2, Count = 5 }, new Item { TypeID = 2, Count = 7 }, new Item { TypeID = 5, Count = 2 } }; I would like to sum up all elements with the same TypeID so that I have a final result list with two elements only: List<Item> list = { new Item { TypeID = 2, Count = 12 }, new Item { TypeID = 5, Count = 2 } }; How can I achive this using LINQ? Cheers Simon 回答1: You can use GroupBy to group by TypeID first

Writing an extension method for Entity Framework

别说谁变了你拦得住时间么 提交于 2021-02-08 10:13:11
问题 I would like to have an extension method called FirstOrDefaultCache() which would check dbContext.EntityName.Local.FirstOrDefault(condition) , and only if that is null, check dbContext.EntityName.FirstOrDefault(condition) . I got the following from another post, which works okay: public static TEntity FirstOrDefaultCache<TEntity>(this DbSet<TEntity> queryable, Expression<Func<TEntity, bool>> condition) where TEntity : class { return queryable .Local.FirstOrDefault(condition.Compile()) // find

Is there any requirement for model class in SQLite database?

假装没事ソ 提交于 2021-02-08 09:52:54
问题 I am writing code using LINQ to SQLite on UWP. To my understanding, models are templates of rows of the tables in the database. The documentations about SQLite barely mention the restrictions of the models, or how the models are converted into rows of tables. The sample codes usually provide only simple cases with primary types. My questions are: Is there any difference between a database model class and a normal class. I find at least in this example(which is a nice one, by the way), The

Expression表达式目录树

…衆ロ難τιáo~ 提交于 2021-02-08 08:59:43
一、初识Expression 源码 1、在上一篇我们讲到了 委托 (忘记了可以在看看,点赞在看养成习惯),今天要讲的Expression也和委托有一点点关系吧(没有直接关系,只是想要大家看看我其他的文章),Expression是.NET准备为Linq to Sql准备的,它的命名空间是System.Linq.Expressions 2、不知道大家有没有用户ORM(对象映射实体)的数据访问层框架,使用过的小伙伴我相信对下面的伪代码不会陌生,我们在Where中传入的就是Expression<Func<TSource, bool>> predicate 3、我们进入Expression一看究竟,我们可以看到Expression<Func<TSource, bool>>里面有一些方法(后面会慢慢道来),最终继承LambdaExpression 4、我们继续进入LambdaExpression,我们看到了一些属性(这些就是我们lambda的组成的方法和属性),但是最终还是看到继承了Expression   5、继续一鼓作气进入Expression,到这里我们看到了最终的基类它里面也有很多方法,要说的话这两天都说不完,我们就简单的介绍一些常用的 二、循序渐进 1、大家可能看了上面还有一点点蒙,不急我们继续,我们看下面的实际操作,我们可以看到我们创建一个Expression和一个委托

How to sum Time from a DataGridView Columns

一个人想着一个人 提交于 2021-02-08 08:59:22
问题 I have a DataGrid with a time column that I need to sum to get the total time, but I can't get that! I'm using this code to try the time sum, with Linq: foreach (DataGridViewRow row in dataGridView1.Rows) { textBox8.Text = dataGridView1.Rows.Cast<DataGridViewRow>() .AsEnumerable() .Sum(x => decimal.Parse(x.Cells["Duracao"].Value.ToString())) .ToString(); } Can anybody help me here? Regards 回答1: Have a look at below code sample. May be it will help you : private void BindGrid() { // Bind Grid

Linq Query To Combine 2 Lists

社会主义新天地 提交于 2021-02-08 06:37:33
问题 I have 2 lists and I need to combine the joining values from A and B, but also include the values from A and B that don't match the join. class TypeA { public string Key { get; set; } public int ValueA { get; set; } } class TypeB { public string Key { get; set; } public int ValueB { get; set; } } class TypeAB { public string Key { get; set; } public int ValueA { get; set; } public int ValueB { get; set; } } var listA = new List<TypeA> { new TypeA { Key = "one", Value = 1 }, new TypeA { Key =

LINQ Many-To-Many Where

谁说胖子不能爱 提交于 2021-02-07 20:50:41
问题 How do I write an EF7 (Core) / SQL Friendly Many-To-Many LINQ Query? Say for example I have many Friends that speak Many Languages, and I want to find all friends for a given set of languages. class Friend { Guid Id { get; set; } ICollection<FriendLanguage> Languages { get; set; } } class Language { Guid { get; set; } ICollection<FriendLanguage> Friends { get; set; } } class FriendLanguage { Friend { get; set; } Language { get; set; } } Given I have a set of language IDs IEnumerable<Guid> , I

get a List of Max values across a list of lists

给你一囗甜甜゛ 提交于 2021-02-07 20:33:27
问题 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