linq

Count number of given object in a list with LINQ

♀尐吖头ヾ 提交于 2020-12-02 10:10:06
问题 I have a list which can contain multiple occurrences of the same object. Now I need to count, how often the given object is contained in this list. int count = 0; foreach (IMyObject item in myList) if (item == object2Count) count++; I am sure that this can be done nicer with LINQ, but LINQ is still a mystery to me. My first question is: How would I count the objects via LINQ and the second question: Will this LINQ version be slower or faster? I am using a ObservableCollection and the number

How do I sum a sub-list in linq?

旧时模样 提交于 2020-12-01 10:47:37
问题 I would like sum a property on a sub list of a list ... example: I have: public class List1 { public List<List2> List2 { get; set; } } public class List2 { public int TotalUnits { get; set; } } In my view I have a List<List1> , so that I want to do something like: <%= Model.List1.Where(x.List2.Any()).Sum(x=>x.TotalUnits) .%> Obviously everything after Sum doesn't work, but is there a way I can do this in a single line without having to create a new Dto on my server side? 回答1: Are you trying

.Net 反序列化原理学习

一个人想着一个人 提交于 2020-12-01 10:23:28
作者:HuanGMz@知道创宇404实验室 时间:2020年11月30日 一.TypeConfuseDelegate工具链 TypeConfuseDelegate 工具链 利用了SortedSet类在反序列化时调用比较器进行排序,以及多播委托可以修改委托实例的特点实现在反序列化时执行代码 0x10 基础知识 0x11 SortedSet<T> SortedSet<T> 从其名字就可以看出其用处,可排序的set,表示按排序顺序维护的对象的集合。<T> 为泛型用法,T表示集合中元素的类型。 既然是可排序的,那么集合中的元素要依据什么进行排序呢?我们看一个SortedSet 的例子就知道了: // Defines a comparer to create a sorted set // that is sorted by the file extensions. public class ByFileExtension : IComparer<string> { string xExt, yExt; CaseInsensitiveComparer caseiComp = new CaseInsensitiveComparer(); public int Compare(string x, string y) { // Parse the extension from the file

C# Entity Framework: Linq Filter out certain GrandChild Elements

放肆的年华 提交于 2020-11-30 01:56:31
问题 How do I filter out Grandchild elements with a Linq EF Query? This Customer has multiple transactions, and only need subchild elements with Certain ProductTypeId. Its currently bringing All ProductType Ids ignoring the filter . var result = db.Customer .Include(b => b.Transactions) .Where(a => a.Transactions.Select(c=> c.ProductTypeId== productTypeId).Any()) Sql query, that I want: select distinct c.customerName from dbo.customer customer inner join dbo.Transactions transaction on transaction

C#--winform窗体控件随窗体变化

喜你入骨 提交于 2020-11-29 16:13:19
C#--winform窗体控件随窗体变化 分类专栏: c# 今天说一下在winform窗体应用中,如何使窗体内控件大小随着窗体变化。因为在默认的情况下,点击最大化的按钮,我们发现窗体内的控件大小不变,这就有些尴尬了! using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Runtime.InteropServices; using System.Threading; namespace Sequence_C_sharp { public partial class Form1 : Form { //------------------------------------------------------ 控件大小随窗体大小变化 private float X; private float Y; private void setTag(Control cons) { foreach (Control con in

Comparing GUID to string in LINQ to Entites throws error [duplicate]

本小妞迷上赌 提交于 2020-11-29 06:18:20
问题 This question already has answers here : Operator '==' cannot be applied to operands of type 'System.Guid' and 'string' in linq to entity (7 answers) Closed 6 years ago . EDIT, this is not a duplicate. The suggested SO links want me to call ToString() but I am running a .COUNT() and trying to do a greater than comparison so calling ToString() is not a correct answer. I am trying to fill a variable using the IF shortcut but when I run it I get the error LINQ to Entities does not recognize the

Comparing GUID to string in LINQ to Entites throws error [duplicate]

僤鯓⒐⒋嵵緔 提交于 2020-11-29 06:17:57
问题 This question already has answers here : Operator '==' cannot be applied to operands of type 'System.Guid' and 'string' in linq to entity (7 answers) Closed 6 years ago . EDIT, this is not a duplicate. The suggested SO links want me to call ToString() but I am running a .COUNT() and trying to do a greater than comparison so calling ToString() is not a correct answer. I am trying to fill a variable using the IF shortcut but when I run it I get the error LINQ to Entities does not recognize the

LINQ : Distinct and Orderby

扶醉桌前 提交于 2020-11-28 04:43:28
问题 I am trying to use LINQ (to EF) to get a DISTINCT list and then sort it. All the examples I found sort the result based on the DISTINCT value. But I want to sort it on a different field. Example: Table with 2 fields (canvasSize and canvasLength); var sizes = (from s in ent.competitors select s.canvasSize).Distinct().OrderBy(x => x); All the examples I found give this type of answer. But it sorts by canvasSize whereas, I want to sort by canvasLength. I'm stuck ... Any tips are greatly

How to Sort this String in Ascending Alphanumeric

可紊 提交于 2020-11-28 03:22:30
问题 I have the following list of string var strTest = new List<string> { "B2", "B1", "B10", "B3" }; I want to sort them as follows "B1, B2, B3, B10". If I use LINQ OrderBy it sorts this way "B1, B10, B2, B3" Please help. Here's my code. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SortingDemo { class Program { static void Main(string[] args) { var strTest = new List<string> { "B2", "B1", "B10", "B3" }; var sort = strTest.OrderBy(x => x); var

DDD领域驱动设计

我们两清 提交于 2020-11-27 04:46:25
有幸参与了一些领域驱动的项目,读了一些文章,也见识了一些不伦不类的架构,感觉对领域驱动有了更进一步的认识。所以今天跟大伙探讨一下领域驱动设计,同时也对一些想要实践领域驱动设计却又无处下手,或者一些正在实践却又说不上领域驱动设计到底好在哪的朋友一些指引方向。当然对于”领域驱动设计”这个主题而言从来不乏争论,所以大家可以在畅所欲言。 为什么要使用领域驱动设计? 从Eric Evans的《 领域驱动设计:软件核心复杂性应对之道 》一书的书名就可以看出这一方法论是为了解决软件核心复杂性的。也就是说软件业务越来越复杂了,领域驱动设计可以让事情变得简单。而实际情况是:领域驱动设计的门槛很高,没有很深厚的面向对象编码能力几乎不可能实践成功。 这一说法是否自相矛盾呢?Martin Fowler在 PoEAA 一书中给了一个有力的解释: 我们把三层架构等除了领域驱动之外的架构方式都可以归纳为以数据为中心的架构方式,在图中是黑色的粗实线; 领域驱动设计在图中是绿色的粗实线。 当软件在开发初期,以数据驱动的架构方式非常容易上手,但是随着业务的增长和项目的推进,软件开发和维护难度急剧升高。 领域驱动设计则在项目初期就处在一个比较难以上手的位置,但是随着业务的增长和项目的推进,软件开发和维护难度平滑上升。 这幅图形象的解释了领域驱动设计和传统的软件架构模式两者在软件开发过程中解决复杂性之间的差异。