C#如何将一个类的实例存储在列表中(C# How to store instances of a class in a list)
我有一个名为User的类。 该类有一个名为Users的静态属性,它是存储所有创建用户的列表。 我希望用户在实例化时自动添加到此列表中,以便将其包含在User类的构造函数块中。
我已经编写了下面的代码,但是当我使用构造函数创建用户时,它们不会添加到“用户”列表中,并且列表仍然为空。
以下是我写的代码。
public class User { private static List<User> _users=new List<User>(); private string _name; public User(string Name) { _name=Name; _users.Add(this); } }
我知道在创建User类的实例后,我可以将每个用户添加到列表中。 但我想知道是否有办法自动执行此操作。
还需要知道在User类中是否有更好的用户列表,或者是创建每个用户的基类。
Removing the question as it's not relevant anymore!
原文:https://stackoverflow.com/questions/40708462
最满意答案
我想你在这里不需要匿名类型。 尝试这个:
var bestPrice = query.Min(x => CalcInterest(amount, term, x.ProductRate.Rate));
编辑:玩弄它我意识到这创造了不同的结果。 您的代码返回anon类型。 这只返回感兴趣的对象。 不保留兴趣数据。
无论哪种方式,使用
Min
来获得最小值。You don't need anonymous types here, I think. Try this:
var bestPrice = query.Min(x => CalcInterest(amount, term, x.ProductRate.Rate));
Edit: playing around with it I realized this created a different result. Your code returns an anon type. This just returns the object which has the lowest interest. The interest data isn't preserved.
Either way, use
Min
to get the minimum.
相关问答
更多-
使用查询语法,您可以引入新的范围变量,它将保存方法参数 from m methods.Where(m => m.ReturnType == typeof(void)) let p = m.GetParameters() where p.Length == 1 && p[0].ParameterType == wantedType select m 方法语法不是那么美丽: methods.Where(m => m.ReturnType == typeof(void)) .Select(m => ...
-
保持动态: dynamic result = new ExpandoObject(); dt.AsEnumerable() .GroupBy(r => r.Field
("type")) .ToList() .ForEach(g=> ((IDictionary )result)["sum" + g.Key.ToUpper()] = g.Sum(r=>r.Field ("cnt"))); Console.WriteLine(result.s ... -
Linq匿名类型(Linq Anonymous Type)[2024-01-19]
我想你在这里不需要匿名类型。 尝试这个: var bestPrice = query.Min(x => CalcInterest(amount, term, x.ProductRate.Rate)); 编辑:玩弄它我意识到这创造了不同的结果。 您的代码返回anon类型。 这只返回感兴趣的对象。 不保留兴趣数据。 无论哪种方式,使用Min来获得最小值。 You don't need anonymous types here, I think. Try this: var bestPrice = query. ... -
Linq,VB - 匿名类型无法转换为匿名类型(Linq, VB - Anonymous type cannot be converted to anonymous type)[2022-10-27]
只需将代码的最后一部分更改为: New With {Key l1.id, Key l1.timestamp} 我测试了代码,它的工作原理。 编辑: 我不知道为什么这对你不起作用,我会发布整个代码以确定。 Dim dsNewFiles = From l1 In list1 _ Where Not (From l2 In list2 _ Select l2.ID, l2.TimeS ... -
NHibernate.Linq NHibernate 2.1.2.4000中有很多东西是行不通的。 您可以使用HQL或ICriteria,或者升级到NHibernate 3.0,或者如果您要使用所有数据,请在Select通过添加ToList强制执行Linq查询。 tags .Select(t = new { t.Name, t.Posts.Count }) .ToList() .OrderBy(x => x.Count); 匿名对象本身就是NHibernate.Linq可以处理的东 ...
-
LINQ GroupBy匿名类型(LINQ GroupBy Anonymous Type)[2023-06-25]
根据文档, 匿名类型是引用类型 : 从公共语言运行库的角度来看,匿名类型与任何其他引用类型没有区别。 因此,它将使用 System.Object实现的那些函数的默认实现(至少对于 基于引用相等性的相等性 )。 编辑 :实际上,根据相同的第一个doco链接,它说: 因为匿名类型上的Equals和GetHashCode方法是根据属性的Equals和GetHashcode方法定义的,所以同一匿名类型的两个实例只有在它们的所有属性相等时才相等。 As per the documentation, an anonym ... -
要使用Contains,必须在IEnumerable
传递该类型的实例。 匿名类型非常困难。 相反,我会使用Any扩展方法重载,它允许您指定比较lambda。 例如 var rows2 = (from t2 in db.TABLE2 where (rows1.Any(x => x.COLUMN_B == t2.COLUMN_A)) select t2; In order to use Contains you must pass an instance of the type in t ... -
你的class必须正确覆盖Equals(object)和GetHashCode() 。 否则,即使它们看起来相同,两个new Params { ... }也不会“相等”。 匿名类型会自动覆盖这两个方法。 您也可以使用struct而不是class因为struct使用System.ValueType中存在的Equals(object)和GetHashCode()的覆盖。 如果你选择一个struct ,考虑使类型不可变 ,即使属性为只读(或使用private set; )。 Your class must ove ...
-
因为您的query变量实际上代表一个查询 ,而不是一组实际的数据。 每次枚举query它都会再次执行操作; 您的声明(分配query变量) 定义查询。 枚举它执行它。 您想要做的是以列表的形式创建查询的内存中表示。 你可以这样做: Dim list = (From X In list Select New With {.Points = 0, X.ID}).ToList() For Each o In list o.Points = 1 Next 这应该会给你你期望的行为。 Because yo ...
-
不能隐式转换类型列表匿名类型1 linq [关闭](cannot implicitly convert type list anonymous type 1 linq [closed])[2022-03-25]
在linq查询中,您正在定义一个无法转换为PersonInfo对象的新类型。 要解决此问题,您需要在select中定义PersonInfo对象。 这样你的return语句就和你的对象一样。 ListnewCustomerList = userList.GroupBy(u=>u.ID) .Select(group=> new PersonInfo() { }).ToList(); 在新的PersonInfo中, ...