如何ToLookup()与多个索引? [英] How ToLookup() with multiple indexes?

查看:141
本文介绍了如何ToLookup()与多个索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到code对C#控制台应用程序下面,使用

Taking into account the code for C# Console application below, using

我应该如何修改它,以替代该行:

how should I modify it in order to substitute the line:

foreach (Product product in productsByCategory[category])

由code线

by the code line

foreach (Product product in productsByCategory[category][Id])

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myQuestion
{
  class Program
  {
    static void Main(string[] args)
    {
      var products = new List<Product>
      {
        new Product { Id = 1, Category = "Garden", Value = 15.0 },
        new Product { Id = 1, Category = "Garden", Value = 40.0 },
        new Product { Id = 3, Category = "Garden", Value = 210.3 },
        new Product { Id = 4, Category = "Pets", Value = 2.1 },
        new Product { Id = 5, Category = "Electronics", Value = 19.95 },
        new Product { Id = 6, Category = "Pets", Value = 21.25 },
        new Product { Id = 7, Category = "Pets", Value = 5.50 },
        new Product { Id = 8, Category = "Garden", Value = 13.0 },
        new Product { Id = 9, Category = "Automotive", Value = 10.0 },
        new Product { Id = 10, Category = "Electronics", Value = 250.0 }
      };


      ILookup<string, Product> productsByCategory = 
              products.ToLookup( p => p.Category);
      string category = "Garden";
      int Id = 1;
      foreach (Product product in productsByCategory[category])
      {
        Console.WriteLine("\t" + product);
      }

      Console.ReadLine();
    }
  }

  public sealed class Product
  {
    public int Id { get; set; }
    public string Category { get; set; }
    public double Value { get; set; }
    public override string ToString()
    {
      return string.Format("[{0}: {1} - {2}]", Id, Category, Value);
    }
  }
}

更新:
这是一个人为的例子与学习C# ToLookup方法。

Update:
This is a contrived example with the aim of learning the concepts of C# ToLookup Method.

作为一个参照点,我来到这个问题阅读大卫·安德烈斯回答问题什么是查找点?

As a point of reference, I came to this question after reading the David Andres' answer to question "What is the point of Lookup?":

"A Lookup will map to potentially several values.  

Lookup["Smith"]["John"] will be a collection of size one billion."   

我想重现。

which I'd like to reproduce.

还是我理解错了?

推荐答案

不知道我理解你的需求正确的,但为什么你就不能这样做:

Not sure I understand your needs correctly, but why can't you just do:

foreach (Product product in productsByCategory[category].Where(x=> x.Id == Id))

或与匿名对象:

Or with an anonymous object:

var productsByCategory = products.ToLookup(p => new { p.Category, p.Id });
string category = "Groceries";
int Id = 1;
foreach (Product product in productsByCategory[new {Category = category, Id= Id}])
{
    Console.WriteLine("\t" + product);
}

下面是非常类似的问题有更多的解决方案,通过 Servy

Here is very similar question with additional solution by Servy

这篇关于如何ToLookup()与多个索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆