我想使用Linq to SQL向数据库添加一些行,但我想在添加行之前进行"自定义检查",以了解是否必须添加,替换或忽略进行的行.我希望尽可能降低客户端和数据库服务器之间的流量,并尽量减少查询次数.
为此,我希望获取验证所需的信息,并且只需要在流程开始时获取一次.
我在考虑做这样的事情,但很明显,它不起作用.有人有想法吗?
Dictionary<int, DateTime> existingItems =
(from ObjType ot in TableObj
select (new KeyValuePair<int, DateTime>(ot.Key, ot.TimeStamp))
)
Run Code Online (Sandbox Code Playgroud)
我最后想要的是一个Dictionary,而不必从TableObject下载整个ObjectType对象.
我还考虑了以下代码,但我试图找到一个正确的方法:
List<int> keys = (from ObjType ot in TableObj orderby ot.Key select ot.Key).ToList<int>();
List<DateTime> values = (from ObjType ot in TableObj orderby ot.Key select ot.Value).ToList<int>();
Dictionary<int, DateTime> existingItems = new Dictionary<int, DateTime>(keys.Count);
for (int i = 0; i < keys.Count; i++)
{
existingItems.Add(keys[i], values[i]);
}
Run Code Online (Sandbox Code Playgroud) 我有一个4边凸多边形由2D中的4个点定义,我希望能够在其中生成随机点.
如果它真的简化了问题,我可以将多边形限制为平行四边形,但更常见的答案是首选.
生成随机点直到其中一个在多边形内部将不起作用,因为它所花费的时间实际上是不可预测的.
假设我有一个C#类库项目,它只包含xml文件作为嵌入式资源.我想从另一个解决方案项目中访问这些资源.由于'class'库不包含类,因此很难获得如下所示的程序集:
typeof(ClassName).Assembly ...
Run Code Online (Sandbox Code Playgroud)
最终获得嵌入式资源.有没有办法获得嵌入式资源,而无需硬编码任何魔术字符串等?谢谢.
PS:
这似乎是目前唯一可能的方式:
var assembly = typeof(FakeClass).Assembly;
var stream = assembly.GetManifestResourceStream("Data.Blas.xml");
Run Code Online (Sandbox Code Playgroud)
我在'数据'程序集中创建了一个'假类'.
长话短说:我有2个对象集合.一个包含好的值(让我们称之为"好"),其他默认值(先生"默认").我希望联盟的相交在良好和默认之间,以及默认.换句话说:相交(联合(良好,默认),默认).有人可能认为它解析为默认值,但这里是棘手的:我使用自定义IEqualityComparer.
我得到了以下课程:
class MyClass
{
public string MyString1;
public string MyString2;
public string MyString3;
}
class MyEqualityComparer : IEqualityComparer<MyClass>
{
public bool Equals(MyClass item1, MyClass item2)
{
if(item1 == null && item2 == null)
return true;
else if((item1 != null && item2 == null) ||
(item1 == null && item2 != null))
return false;
return item1.MyString1.Equals(item2.MyString1) &&
item1.MyString2.Equals(item2.MyString2);
}
public int GetHashCode(MyClass item)
{
return new { item.MyString1, item.MyString2 }.GetHashCode();
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我的收藏品Good和Default集合的特征:
默认值:它是一个很大的集合,包含所有想要的{MyString1,MyString2}对,但是你可以猜测,MyString3值是默认值.
好:它是一个较小的集合,主要包含默认集合中的项目,但具有一些好的MyString3值.它还有一些{MyString1,MyString2},它们位于想要的集合之外.
我想要做的是:只获取Good中默认的项目,但将Default中的其他项目添加到默认项目中.
这是我认为最好的尝试:
HalfWantedResult = Good.Union(Default, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用RijndaelManaged通过套接字加密和解密文件流,但我一直在碰到异常
CryptographicException: Length of the data to decrypt is invalid.
at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
当整个文件被传输时,在receiveFile中的using语句结束时抛出异常.
我尝试在网上搜索,但只找到了在加密和解密单个字符串时使用编码时出现的问题的答案.我使用FileStream,所以我没有指定要使用的任何编码,所以这不应该是问题.这些是我的方法:
private void transferFile(FileInfo file, long position, long readBytes)
{
// transfer on socket stream
Stream stream = new FileStream(file.FullName, FileMode.Open);
if (position > 0)
{
stream.Seek(position, SeekOrigin.Begin);
}
// if this should be encrypted, wrap the encryptor stream
if (UseCipher)
{
stream = new CryptoStream(stream, streamEncryptor, CryptoStreamMode.Read);
}
using (stream)
{
int read;
byte[] array = …Run Code Online (Sandbox Code Playgroud) 是否可以通过某种SMART API或类似的东西来查看Harrdisk温度?
我只想要临时,没有别的C#
使用awk/ 删除字符串中的第一个和最后一个字符相当容易sed吗?
说我有这个字符串
( 1 2 3 4 5 6 7 )
我想从中删除括号.
我该怎么做?
如果你有对象Dictionary<k, v> myDictionary,那么myDictionary.Values将是类型Dictionary<k, v>.ValueCollection并且myDictionary.Keys将是类型Dictionary<k, v>.KeyCollection.
我不明白为什么类型myDictionary.Values不是IEnumerable<v>,IList<v>或其他东西.
现在,考虑到这一点,如果我创建一个自定义类型的字典; Dictionary2<k1, k2, v>,应该myCustomDictionary.Values返回一个IEnumerable<v>,还是ValueCollection的自定义工具?更重要的是,为什么?
我有一个字符串
var string = "I have a a string \\nThis is a new line\\nThis is another";
Run Code Online (Sandbox Code Playgroud)
我想要的是什么
var string = "I have a a string \nThis is a new line\nThis is another";
Run Code Online (Sandbox Code Playgroud)
我用过
string.Replace("\\","\");
Newline in constant
Run Code Online (Sandbox Code Playgroud)
但得到错误消息"Newline in constant"
c# ×8
linq ×2
.net ×1
2d ×1
algorithm ×1
awk ×1
collections ×1
cryptography ×1
destructor ×1
dictionary ×1
dispose ×1
finalizer ×1
idisposable ×1
intersect ×1
intersection ×1
linq-to-sql ×1
matlab ×1
polygon ×1
random ×1
sed ×1
string ×1
todictionary ×1