在.NET GetHashCode方法中,很多地方都使用.NET 方法.特别是在快速查找集合中的项目或确定相等性时.是否有关于如何GetHashCode为我的自定义类实现覆盖的标准算法/最佳实践,因此我不会降低性能?
我有一个Person对象列表.我想转换为一个字典,其中键是名字和姓氏(连接),值是Person对象.
问题是我有一些重复的人,所以如果我使用这个代码会爆炸:
private Dictionary<string, Person> _people = new Dictionary<string, Person>();
_people = personList.ToDictionary(
e => e.FirstandLastName,
StringComparer.OrdinalIgnoreCase);
Run Code Online (Sandbox Code Playgroud)
我知道这听起来很奇怪,但我现在并不关心重复的名字.如果有多个名字我只想抓一个.无论如何,我可以在上面写这个代码,所以它只需要一个名字,并没有炸毁重复?
我有从数据库派生的以下EF类(简化)
class Product
{
public string ProductId;
public string ProductName;
public string CategoryId;
public string CategoryName;
}
Run Code Online (Sandbox Code Playgroud)
ProductId是表的主键.
对于DB设计者做出的糟糕的设计决策(我无法修改它),我有CategoryId和CategoryName在这个表中.
我需要一个DropDownList的使用(不同)CategoryId的价值,并CategoryName为文本.因此我应用了以下代码:
product.Select(m => new {m.CategoryId, m.CategoryName}).Distinct();
Run Code Online (Sandbox Code Playgroud)
从逻辑上讲,它应该使用CategoryId和CategoryName作为属性创建一个匿名对象.在Distinct()保证有没有重复对(CategoryId,CategoryName).
但实际上它不起作用.据我所知,Distinct()当集合中只有一个字段时,它就会忽略它们......这是正确的吗?有没有解决方法?谢谢!
UPDATE
对不起product是:
List<Product> product = new List<Product>();
Run Code Online (Sandbox Code Playgroud)
我找到了另一种获得相同结果的方法Distinct():
product.GroupBy(d => new {d.CategoryId, d.CategoryName})
.Select(m => new {m.Key.CategoryId, m.Key.CategoryName})
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Moq验证方法调用,但我无法正确地获得语法.目前我已将此作为我的验证:
repository.Verify(x => x.ExecuteNonQuery("fav_AddFavorites", new
{
fid = 123,
inputStr = "000456"
}), Times.Once());
Run Code Online (Sandbox Code Playgroud)
代码编译,但测试失败并出现错误:
Expected invocation on the mock once, but was 0 times:
x => x.ExecuteNonQuery("fav_AddFavorites", new <>f__AnonymousType0<Int32, String>(123, "000456"))
No setups configured.
Performed invocations:
IRepository.ExecuteNonQuery("fav_AddFavorites", { fid = 123, inputStr = 000456 })
Run Code Online (Sandbox Code Playgroud)
如何验证方法调用并匹配匿名类型的方法参数.
UPDATE
回答问题:
我试图验证方法被调用和参数是否正确.
我试图验证的方法的签名是:
int ExecuteNonQuery(string query, object param = null);
Run Code Online (Sandbox Code Playgroud)
设置代码很简单:
repository = new Mock<IRepository>();
Run Code Online (Sandbox Code Playgroud)
更新2
看起来这是Moq的一个问题,以及它如何处理.Net中的匿名类型.Paul Matovich发布的代码运行良好,但是,一旦代码和测试在不同的程序集中,测试就会失败.
我有一个 LINQ2SQL 语句,其中使用两个条件:
var query1 = from r in dt.Test
where r.ID == 92
&& r.Status = '"I"
select r.ID && r.Status = "I"
Run Code Online (Sandbox Code Playgroud)
但它给了我一个错误,即 AND (&&) 运算符不能与字符串和布尔值一起使用。这有什么转折呢?
我如何才能完成这项独特的工作:
var blockIdMap = (from panelEntry in panelEntries
select new {panelEntry.BlockNo, panelEntry.BlockID})
.Distinct()
.ToDictionary(mc => mc.BlockNo , mc => mc.BlockID);
Run Code Online (Sandbox Code Playgroud)
我需要只有BlockNo的唯一条目和它的BlockId,因为我将它们输入到Dictionary并且BlockNo应该是唯一的.我只想拿第一个.
Distinct()如何在匿名类型的List <>上工作?它会做一个财产比较吗?还是总是返回相同的列表?
例:
List<SomeObject> list;
....
....
var result = list
.Where(i => i.Condition)
.Select(i => new
{
Name = i.Name,
Date = i.Date
});
.Distinct()
.ToList()
Run Code Online (Sandbox Code Playgroud)
请注意,我在匿名类型列表中应用了非重复。
基本上我正在尝试在LINQ to SQL中执行此操作;
SELECT DISTINCT a,b,c FROM table WHERE z=35
Run Code Online (Sandbox Code Playgroud)
我试过这个,(c#代码)
(from record in db.table
select new table {
a = record.a,
b = record.b,
c = record.c
}).Where(record => record.z.Equals(35)).Distinct();
Run Code Online (Sandbox Code Playgroud)
但是当我以这种方式从表对象中删除列z时,我得到以下异常;
绑定错误:在投影中找不到成员'table.z'.
我不能返回字段z,因为它会使我的明显无用.任何帮助表示赞赏,谢谢.
编辑:
这是一个更全面的例子,包括使用PredicateBuilder,
var clause = PredicateBuilder.False<User>();
clause = clause.Or(user => user.z.Equals(35));
foreach (int i in IntegerList) {
int tmp = i;
clause = clause.Or(user => user.a.Equals(tmp));
}
var results = (from u in db.Users
select new User {
a = user.a,
b = user.b,
c = …Run Code Online (Sandbox Code Playgroud) c# ×6
linq ×4
linq-to-sql ×2
.net ×1
algorithm ×1
dictionary ×1
distinct ×1
gethashcode ×1
hashcode ×1
moq ×1
sql ×1