我正在从另一个数据库导入数据。
我的过程是将数据从远程数据库导入到List<DataModel>命名的数据库中,并将remoteData数据从本地数据库导入到List<DataModel>命名的localData.
然后我使用 LINQ 创建一个不同的记录列表,以便我可以更新本地数据库以匹配从远程数据库中提取的数据。像这样:
var outdatedData = this.localData.Intersect(this.remoteData, new OutdatedDataComparer()).ToList();
Run Code Online (Sandbox Code Playgroud)
然后我使用 LINQ 创建一个不再存在于remoteData中但确实存在于中的记录列表localData,以便我将它们从本地数据库中删除。
像这样:
var oldData = this.localData.Except(this.remoteData, new MatchingDataComparer()).ToList();
Run Code Online (Sandbox Code Playgroud)
然后我使用 LINQ 执行与上述相反的操作,将新数据添加到本地数据库。
像这样:
var newData = this.remoteData.Except(this.localData, new MatchingDataComparer()).ToList();
Run Code Online (Sandbox Code Playgroud)
每个集合导入大约 70k 条记录,3 个 LINQ 操作中的每一个都需要 5 到 10 分钟才能完成。我怎样才能更快?
这是集合正在使用的对象:
internal class DataModel
{
public string Key1{ get; set; }
public string Key2{ get; set; }
public string Value1{ get; set; }
public string Value2{ …Run Code Online (Sandbox Code Playgroud) 我有一个类,它具有许多我正在实现的属性IEquitable<T>.我找到了关于如何为少量属性执行GetHashCode()的多个示例.
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + field1.GetHashCode();
hash = hash * 23 + field2.GetHashCode();
hash = hash * 23 + field3.GetHashCode();
return hash;
}
}
Run Code Online (Sandbox Code Playgroud)
当我在对象上有数百个属性时,我该怎么回事?
我需要使用x,y,z坐标(仅限整数)对大型bool集进行查找.我试图确定一个块是否可以在x,y,z位置遍历.
目前我正在使用数组bool[,,].然而,这不是非常灵活,并且对于尺寸合适的地图,尺寸很快变得很大.
我认为只有真正价值观的字典会更灵活,内存更少.我创建了一个结构Vector3Int来保存x,y,z值并用作字典键.字典看起来像Dictionary<Vector3Int, bool>.
但是,使用此键进行字典查找似乎比使用x,y,z整数的数组查找慢20-100倍.
有没有更快/更好的方法来做到这一点?我使用查找进行路径查找,因此查找需要非常快,因为单个路径可能有数百/数千个查找.
Vector3Int代码:
public struct Vector3Int
{
public int x,y,z;
public Vector3Int(int x, int y, int z)
{
this.x =x;
this.y=y;
this.z=z;
}
//checks for equality
public static bool operator ==(Vector3Int a, Vector3Int b)
{
return a.x==b.x && a.y ==b.y && a.z ==b.z;
}
public override bool Equals(object obj)
{
return obj is Vector3Int && this == (Vector3Int)obj;
}
public override int GetHashCode ()
{
return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
} …Run Code Online (Sandbox Code Playgroud) 使用时IMemoryCache用object,TryGetValue永远怀念。我正在尝试将 atuple<string, object>作为关键,并且 atuple<string, string>工作得很好。
这里的代码总是让我缓存未命中:
_cache.TryGetValue(("typeOfCache", query), out var something);
if(something == null) _cache.CreateEntry(("typeOfCache", query));
Run Code Online (Sandbox Code Playgroud)
我使用的对象里面有列表列表,而不是没有字典/集合(没有随机排序的东西)。
这是 .net 错误还是我做错了什么?
我遇到了从这几个类和接口中获得所需行为的问题.
这是我的问题,
//Inside a Unit Test that has access to internal methods and properties
INode firstNode, secondNode;
INodeId id = new NodeId (4);
first = new Node (id, "node");
second = new Node (id, "node");
Assert.IsTrue (first == second);
Run Code Online (Sandbox Code Playgroud)
上面的断言失败了,因为它似乎是在对象类的equals方法而不是Node和NodeId类中的重载运算符.
如果您对如何获得所需行为有任何建议,那将是非常棒的.
以下是我正在研究的框架的一部分:
public interface IIdentifier<T> where T : class
{
TKeyDataType GetKey<TKeyDataType> ();
bool Equals (IIdentifier<T> obj;
}
public interface INode
{
string name
{
get;
}
INodeId id
{
get;
}
}
public interface INodeId : IIdentifier<INode>
{
} …Run Code Online (Sandbox Code Playgroud) 首先,我在这里使用所GetHashCode描述的算法.现在,想象下面的(人为的)示例:
class Foo
{
public Foo(int intValue, double doubleValue)
{
this.IntValue = intValue;
this.DoubleValue = doubleValue;
}
public int IntValue { get; private set; }
public double DoubleValue { get; private set; }
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 23 + IntValue.GetHashCode();
hash = hash * 23 + DoubleValue.GetHashCode();
return hash;
}
}
}
class DerivedFoo : Foo
{
public DerivedFoo(int intValue, double doubleValue)
: base(intValue, doubleValue) …Run Code Online (Sandbox Code Playgroud) 我有一个列表,我想过滤重复的项目.在询问这个问题之前,我在StackOverflow上搜索并找到了两个解决方案; 使用.Distinct()和使用HashSet,但这些方法都不适合我.我试图过滤的对象实现了该.Equals方法,但它仍然无效.
我通过创建500个完全相同的对象并将它们放在列表中来测试它.我预计会有1人离开,但所有500人仍在那里.我的对象是否需要实现其他方法才能工作?
谢谢.
我一直在读这篇文章,因为它是由Jon Skeet在这个答案上联系起来的.我试图真正理解哈希是如何工作的以及为什么乔恩喜欢他提供的算法.我还没有声称对此有答案,但我确实有一个关于基本System.String实现的具体问题GetHashCode.
考虑代码,重点是注释<<<<<==========行:
public override unsafe int GetHashCode()
{
if (HashHelpers.s_UseRandomizedStringHashing)
return string.InternalMarvin32HashString(this, this.Length, 0L);
fixed (char* chPtr = this)
{
int num1 = 352654597;
int num2 = num1;
int* numPtr = (int*) chPtr;
int length = this.Length;
while (length > 2)
{
num1 = (num1 << 5) + num1 + (num1 >> 27) ^ *numPtr;
num2 = (num2 << 5) + num2 + (num2 >> 27) ^ numPtr[1];
numPtr …Run Code Online (Sandbox Code Playgroud) 我有一个字典,其中键是XYZ对象,值是boolean.XYZ类来自Autodesks API,因此它不是我制作的类.我试图检查字典中是否存在密钥.
我的问题:如果字典包含密钥new XYZ(1,1,1),我会检查字典是否包含此密钥,使用myDictionary.ContainsKey(new XYZ(1,1,1)始终返回false.
为什么会发生这种情况?我该如何解决这个问题?我认为该类XYZ需要Equals实现它的方法,但正如我之前提到的,我没有创建这个类,它是Autodesks API的一部分.或者我做错了什么?
Dictionary<XYZ, bool> prevPnts = new Dictionary<XYZ, bool>();
prevPnts[new XYZ(1,1,1)] = true;
// Always says the pnt doesnt exist?
if (prevPnts.ContainsKey(new XYZ(1,1,1)))
TaskDialog.Show("Contains");
else TaskDialog.Show("NOT Contains");
Run Code Online (Sandbox Code Playgroud)
解决方案使用Konrads的答案
class XYZEqualityComparer : IEqualityComparer<XYZ>
{
public bool Equals(XYZ a, XYZ b)
{
if (Math.Abs(a.DistanceTo(b)) <= 0.05)
return true;
return false;
}
public int GetHashCode(XYZ x)
{
int hash = 17;
hash = hash * 23 …Run Code Online (Sandbox Code Playgroud) 在 C# 中,我想要一个将 (x,y) 坐标映射到 (x,y) 的数据结构。我怎样才能做这样的事情?
我不想使用类似 的公式将 x,y 坐标转换为单个值y*w+x。有没有办法可以拥有dictionary<key,key,(value,value)>.
如果我将键作为 Tuple,那么它是一个对象,并且 Tuple(1,1) 不等于 Tuple(1,1)。所以我认为我无法找到这个意义上的钥匙。
c# ×10
algorithm ×2
dictionary ×2
.net ×1
arrays ×1
autodesk ×1
caching ×1
collections ×1
gethashcode ×1
hash ×1
hashcode ×1
hashset ×1
inheritance ×1
linq ×1
list ×1
memorycache ×1
performance ×1