如何使用LINQ在另一个IEnumerable内的IEnumerable中选择属性?

时间:2019-04-14 16:15:51

标签: c# linq

我有这个LINQ查询,它返回下图所示的数据:

var result = jmdict
            .Where(x => x.Sequence == 1438690)
            .SelectMany(entry =>
                entry.Senses.Select(s =>
                    (entry.Sequence, s.Glosses.Where(x => x.Language.Code == "eng"))));

enter image description here

有人知道如何修改它,以便item2仅返回Term(“天气”,“元素”等)值,而不是当前返回的IEnumerator。

1)我尝试了早些时候给我的这个建议

var r3 = 
   jmdict.Where(x => x.Sequence == 1438690)
         .SelectMany(entry => entry.Senses.Select(s => s.Glosses.Where(x => x.Language.Code == "eng")
         .Select(y => (entry.Sequence, y => y.Term))));

但是这不起作用,最后一个Select下方有一个错误行,给出此错误消息:

  

GetDetails.cs(155,155):错误CS0411:方法的类型参数   'Enumerable.Select(IEnumerable,   无法从用法中推断出Func)'。尝试   明确指定类型参数。 (CS0411)(下载)

public static List<IJapaneseDictionaryEntry> jmdict; 

public class JapaneseDictionaryEntry : IJapaneseDictionaryEntry {
    public int Sequence { get; set; }
    private readonly List<Sense> senses = new List<Sense>();
    public IEnumerable<ISense> Senses => this.senses;
}
public interface ISense {
    IEnumerable<Gloss> Glosses { get; }
}
public class Gloss {
    public string Term { get; }
    public Language Language { get; }
    public Gloss(string term, Language language, string gender)
    {
        this.Term = term;
        this.Language = language;
        this.Gender = gender;
    }
}

1 个答案:

答案 0 :(得分:1)

最后,这是您更正的第二个查询

var r3 = jmdict.Where(x => x.Sequence == 1438690)
     .SelectMany(entry => entry.Senses.SelectMany(s => s.Glosses.Where(x => x.Language.Code == "eng")
     .Select(y => (entry.Sequence, y => y.Term))));

我已经采用了您的架构并进行了一些修改,这里将是

{Sequence =?,Term =?}

data.Where(a=>a.Sequence==2)
                       .SelectMany(b=>b.Senses.SelectMany(x=>x.Glosses.Where(g => g.Language == "German"))
                       .Select(y => new {b.Sequence,  y.Term}));

public class JapaneseDictionaryEntry
{
    private List<Sense> senses = new List<Sense>();
    public int Sequence { get; set; }

    public List<Sense> Senses { get { return senses; } set { senses = value; } }
}
public class Sense
{
    private List<Gloss> glosses = new List<Gloss>();

    public List<Gloss> Glosses { get { return glosses; } set { glosses = value; } }
}
public class Gloss
{
    public string Term { get; set; }
    public string Language { get; set; }

}
class Program
{
    static List<JapaneseDictionaryEntry> GetData()
    {
        return new List<JapaneseDictionaryEntry>() {
                    new JapaneseDictionaryEntry()
                    {
                        Sequence = 1,
                        Senses = new List<Sense>() { new Sense() { Glosses = new List<Gloss>() {
                            new Gloss() { Term = "1",Language="English"},
                            new Gloss() { Term = "2",Language="German" },
                            new Gloss() { Term = "3",Language="German" },
                            new Gloss() { Term = "4",Language="English" }
                        }
                        } }
                    },
                    new JapaneseDictionaryEntry()
                    {
                        Sequence = 2,
                        Senses = new List<Sense>() { new Sense() { Glosses = new List<Gloss>() {
                            new Gloss() { Term = "a", Language="English"},
                            new Gloss() { Term = "b", Language="German" },
                            new Gloss() { Term = "c", Language="German" },
                            new Gloss() { Term = "d", Language="English"}
                        }
                        } }
                    }
                };
    }

    static void Main(string[] args)
    {
        var data = GetData();
        var termData = data.Where(a=>a.Sequence==2)
                           .SelectMany(b=>b.Senses.SelectMany(x=>x.Glosses.Where(g => g.Language == "German"))
                           .Select(y => new {b.Sequence,  y.Term}));

        foreach (var item in termData)
        {
            Console.WriteLine(item);
        }

    }}