C# 将 Linq 查询结果转换为字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/953919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:08:33  来源:igfitidea点击:

Convert Linq Query Result to Dictionary

c#linqlinq-to-sqltodictionary

提问by Tipx

I want to add some rows to a database using Linq to SQL, but I want to make a "custom check" before adding the rows to know if I must add, replace or ignore the incomming rows. I'd like to keep the trafic between the client and the DB server as low as possible and minimize the number of queries.

我想使用 Linq to SQL 向数据库添加一些行,但我想在添加行之前进行“自定义检查”,以了解是否必须添加、替换或忽略传入的行。我想保持客户端和数据库服务器之间的流量尽可能低,并尽量减少查询次数。

To do this, I want to fetch as little information as required for the validation, and only once at the beginning of the process.

为此,我希望获取验证所需的尽可能少的信息,并且仅在流程开始时获取一次。

I was thinking of doing something like this, but obviously, it doesn't work. Anyone have an idea?

我正在考虑做这样的事情,但显然,它不起作用。有人有想法吗?

Dictionary<int, DateTime> existingItems = 
    (from ObjType ot in TableObj
        select (new KeyValuePair<int, DateTime>(ot.Key, ot.TimeStamp))
    )

What I'd like to have at the end would be a Dictionary, without having to download the whole ObjectType objects from TableObject.

最后我想要的是一个字典,而不必从 TableObject 下载整个 ObjectType 对象。

I also considered the following code, but I was trying to find a proper way:

我还考虑了以下代码,但我试图找到一种正确的方法:

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]);
}

采纳答案by tvanfosson

Try using the ToDictionarymethodlike so:

尝试使用如下ToDictionary方法

var dict = TableObj.ToDictionary( t => t.Key, t => t.TimeStamp );

回答by JaredPar

Try the following

尝试以下

Dictionary<int, DateTime> existingItems = 
    (from ObjType ot in TableObj).ToDictionary(x => x.Key);

Or the fully fledged type inferenced version

或者完全成熟的类型推断版本

var existingItems = TableObj.ToDictionary(x => x.Key);

回答by BFree

Looking at your example, I think this is what you want:

看看你的例子,我认为这就是你想要的:

var dict = TableObj.ToDictionary(t => t.Key, t=> t.TimeStamp);

回答by Salman Mushtaq

Use namespace

使用命名空间

using System.Collections.Specialized;

Make instance of DataContextClass

制作DataContext类的实例

LinqToSqlDataContext dc = new LinqToSqlDataContext();

Use

OrderedDictionary dict = dc.TableName.ToDictionary(d => d.key, d => d.value);

In order to retrieve the values use namespace

为了检索值使用命名空间

   using System.Collections;

ICollection keyCollections = dict.Keys;
ICOllection valueCollections = dict.Values;

String[] myKeys = new String[dict.Count];
String[] myValues = new String[dict.Count];

keyCollections.CopyTo(myKeys,0);
valueCollections.CopyTo(myValues,0);

for(int i=0; i<dict.Count; i++)
{
Console.WriteLine("Key: " + myKeys[i] + "Value: " + myValues[i]);
}
Console.ReadKey();