1
votes

I have this LINQ Query which returns the data shown in the image below:

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

Does anyone have any ideas how I could modify this so that item2 would return just the value of Term ("weather","the elements" etc) instead of the IEnumerator which it currently returns.

1) I tried this suggestion which was given to me earlier

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

But this does not work and there's an error line under the last Select giving this error message :

GetDetails.cs(155,155): Error CS0411: The type arguments for method 'Enumerable.Select(IEnumerable, Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly. (CS0411) (Download)

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
Is it possible to provide sample data as json?Risto M
Looks like you need to replace .Senses.Select with .Senses.SelectMany in the suggested code. You ought to use SelectMany anytime you need to convert a enumerable of enumerables into a single enumerable.tukaef

1 Answers

2
votes

Finally Here is your corrected second query

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

I have taken your schema and modified a bit, here out put will be

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

    }}