Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在字典上使用LINQ Where子句时,如何返回相同类型的字典?_C#_Linq_Optimization - Fatal编程技术网

C# 在字典上使用LINQ Where子句时,如何返回相同类型的字典?

C# 在字典上使用LINQ Where子句时,如何返回相同类型的字典?,c#,linq,optimization,C#,Linq,Optimization,我目前正在使用以下代码来实现这一点,但似乎应该有更好的东西。。。建议?在我看来,应该有一种方法可以跳过foreach Dictionary<string,string> getValidIds(Dictionary<string,string> SalesPersons,List<string> ids) { Dictionary<string,string> o = new Dictionary<string,string>(

我目前正在使用以下代码来实现这一点,但似乎应该有更好的东西。。。建议?在我看来,应该有一种方法可以跳过foreach

Dictionary<string,string> getValidIds(Dictionary<string,string> SalesPersons,List<string> ids)
{
    Dictionary<string,string> o = new Dictionary<string,string>();
    var ie = SalesPersons.Where<KeyValuePair<string, string>>(t => ids.Contains(t.Key));
    foreach (var i in ie)
    {
        o.Add(i.Key, i.Value);
    }
    return o;
}
Dictionary GetValidID(字典销售人员、列表ID)
{
字典o=新字典();
var ie=salersons.Where(t=>ids.Contains(t.Key));
foreach(ie中的var i)
{
o、 添加(i.键,i.值);
}
返回o;
}

非常确定您可以根据Where调用的结果调用ToDictionary:

Dictionary<string, string> GetValidIds(Dictionary<string, string> salesPersons,
    IList<string> ids)
{
    return salesPersons
        .Where(p => ids.Contains(p.Key))
        .ToDictionary(p => p.Key, p => p.Value);
}
Dictionary GetValidis(字典销售员,
IList ID)
{
退货销售员
.Where(p=>ids.Contains(p.Key))
.ToDictionary(p=>p.Key,p=>p.Value);
}

有趣的是,如果您首先枚举字典(长度N)并检查列表(长度M)是否包含,那么您将获得O(NM)性能

您可以构建ID的
HashSet
,但这似乎是多余的,因为我们已经有一个(预哈希)字典可用

相反,我会先迭代ID;由于字典查找(按键)是O(1),这会提供O(M)性能-然而,这可能意味着您不使用LINQ(因为
TryGetValue
不会喜欢LINQ(并且引入元组太像是一项艰苦的工作)


在列表中查找内容似乎有很多麻烦。如果列表只包含少数元素,则没有什么大不了的。如果列表包含数千个元素,则需要对其进行O(1)次查找。HashSet可以提供此功能

Dictionary<string, string> getValidIds(
  Dictionary<string, string> SalesPersons,
  List<string> ids
)
{
  HashSet<string> idsFast = new HashSet<string>(ids);
  Dictionary<string, string> result = SalesPersons
    .Where(kvp => idsFast.Contains(kvp.Key))
    .ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
  return result;
}
字典getvalidis(
字典销售员,
列表ID
)
{
HashSet idsFast=新的HashSet(ids);
字典结果=销售人员
.Where(kvp=>idsFast.Contains(kvp.Key))
.ToDictionary(kvp=>kvp.Key,kvp=>kvp.Value)
返回结果;
}

+1,马克,尽管我认为如果你担心这类事情的性能,你的内存已经太多了!:)@Matt。。。好吧,也许-但是有一点O(1)/O(Nlog(N))*不是不合理的,但是O(N^2)是一个问题-当然,是1k、10k还是100k取决于具体的过程。
    Dictionary<string, string> getValidIds(
        IDictionary<string, string> salesPersons,
        IEnumerable<string> ids)
    {
        string value = null;
        return  (from key in ids
                where salesPersons.TryGetValue(key, out value) // HACK: v. dodgy
                select new { key, value })
                .ToDictionary(x=>x.key, x=>x.value);
    }
Dictionary<string, string> getValidIds(
  Dictionary<string, string> SalesPersons,
  List<string> ids
)
{
  HashSet<string> idsFast = new HashSet<string>(ids);
  Dictionary<string, string> result = SalesPersons
    .Where(kvp => idsFast.Contains(kvp.Key))
    .ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
  return result;
}