C# LINQ join between multiple lists -


i have linq joining statement in c# i'm trying linq list of structs 2 lists of strings:

public struct ll {     public int index;     public string one;     public string two; }  public list <ll> list1;  //{"0,a, ", "1,," , "2,a,dog" , "3,,horse"}  public list <string> list2;    //{"a","b"} public list <string> list3;    //{"cat","mouse","dog"} 

the idea in list1 not structs contains value, on index 0 list1.one has value being "a", list1.two empty. on index 1 both list1.one , list1.two empty. on index 2 both list1.one , list.two have values. on index 3 list.two has value.

with below i'm trying join 3 lists

        var query = x in list1                     join y1 in list2 on x.one equals y1                     join y2 in list3 on x.two equals y2                     select x.index;  

with statement join done if list1.one matches item in list2 , list1.two matches item in list2. see if list1.one filled, list1.two empty, join match item in list2 list1.one, join items in list3, taking empty list1.two not in account.

edit:

let me rephrase question. objective program reads sentence typed in user, stores infinitives, nouns, articles , adverbs in seperate lists , tries find best answer or reply user based on join of lists.

so list1 should like

    public struct ll     {        int index;        string infinitive;        string noun;        string article;        string adverb;        string answer;      } public list <ll> list1; 

with below data:

enter image description here

when example user inputs sentence 'what can find' join script should return 2 possible answers:

"i can find information" , "what want me find"

on sentence "what can find here"

it return: "i can find information"

but when user askes: "what know hobbit" return nothing combination "know" , "this" unknown, "what know hobbit" return "this know about"

hope more clear know

it's hard tell want, , how index defined, i'm guessing this. want l1 kind of input list of search terms per domain, , want l2, l3, etc kind of search domain.

join not right way go this. you're wanting linear search through inputs, domain matches. can nicely abstract by, rather having each domain own list, make list-of-lists, , have searched in order. follows:

var lists = new[]                 {                     new[] { "a", null },                     new string[] { null, null },                     new[] { "a", "dog" },                     new[] { null, "horse" }                 }; var domains = new[] { new[] { "a", "b" }, new[] { "cat", "mouse", "dog" } };  var matches = lists.where(list => list.where((s, i) => s == null || domains[i].contains(s)).count() == list.length);  foreach (var list in matches) {     console.writeline(string.join(",", list)); } 

output:

"a",null null,null "a","dog" 

then can write function determines corresponding index value each list in matches, if that's need. recommend redefine index bitflag rather ad-hoc way describe; can do:

static int getindex<t>(ienumerable<t> list) {     return list.select((s, i) => s == null ? 0 : 1 << i).sum(); } 

or if want inline it's simply

var indices = lists.where(list => list.where((s, i) => s == null || domains[i].contains(s)).count() == list.length)                    .select(list => list.select((s, i) => s == null ? 0 : 1 << i).sum())                    .distinct(); 

or if define nice helper indexed-all linq function, maybe makes things more clear:

public static bool all<t>(this ienumerable<t> seq, func<t, int, bool> pred) {     return seq.select((x, i) => tuple.create(x, i)).all(t => pred(t.item1, t.item2)); } 

...

ienumerable<int> indices = lists     .where(list => list.all((s, i) => s == null || domains[i].contains(s)))     .select(list => list.select((s, i) => s == null ? 0 : 1 << i).sum())     .distinct(); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -