小编Tip*_*ipx的帖子

将Linq查询结果转换为字典

我想使用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)

c# linq todictionary linq-to-sql

327
推荐指数
3
解决办法
31万
查看次数

平行四边形内的随机点

我有一个4边凸多边形由2D中的4个点定义,我希望能够在其中生成随机点.

如果它真的简化了问题,我可以将多边形限制为平行四边形,但更常见的答案是首选.

生成随机点直到其中一个在多边形内部将不起作用,因为它所花费的时间实际上是不可预测的.

random algorithm matlab 2d polygon

48
推荐指数
3
解决办法
3万
查看次数

如何将资源嵌入另一个项目中

假设我有一个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)

我在'数据'程序集中创建了一个'假类'.

.net c#

20
推荐指数
1
解决办法
1万
查看次数

使用Linq与自定义IEqualityComparer相交

长话短说:我有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)

c# linq intersection intersect

14
推荐指数
2
解决办法
2万
查看次数

要解密的数据长度无效

我正在尝试使用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)

c# cryptography rijndaelmanaged

12
推荐指数
1
解决办法
3万
查看次数

如何阅读HardDisk温度?

是否可以通过某种SMART API或类似的东西来查看Harrdisk温度?

我只想要临时,没有别的C#

c# hardware-interface

12
推荐指数
1
解决办法
4532
查看次数

从String中删除最后一个和第一个字符

使用awk/ 删除字符串中的第一个和最后一个字符相当容易sed吗?

说我有这个字符串 ( 1 2 3 4 5 6 7 )

我想从中删除括号.

我该怎么做?

awk sed

11
推荐指数
2
解决办法
3万
查看次数

使用基类的析构函数/处理?

在C#中,如文档中所提到的,以及这篇不错的帖子已接受的答案,它声明类不继承其父类的析构函数.

问题:如果我想确保处理基类的私有元素,是在所有子类中实现IDisposable的正确方法,并在Dispose方法中调用base.Dispose()?

这样做看起来没问题,但我更倾向于一种不需要在所有子类中实现的方法.

c# dispose destructor idisposable finalizer

9
推荐指数
2
解决办法
7728
查看次数

myCustomDictionary.Values应返回什么类型?

如果你有对象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的自定义工具?更重要的是,为什么?

c# collections dictionary language-concepts

7
推荐指数
1
解决办法
69
查看次数

在C#中的字符串中将\n替换为\n

我有一个字符串

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# string

6
推荐指数
2
解决办法
6652
查看次数