简体   繁体   中英

Linq complex Inner Join

I want to join my data with linq inner join as:

[DataContract]
public class Data
{
    [DataMember(Order = 0, IsRequired = false, EmitDefaultValue = false)]
    public List<DataResultObject> Row { get; set; }
}

[DataContract]
public class DataResultObject
{
    [DataMember]
    public string NAME { get; set; }
    [DataMember]
    public string VALUE { get; set; }
    [DataMember]
    public string TYPE { get; set; }

}

List<Data> follow = (List<Data>)dataset_cache.Get("follow");//364 rows
List<Data> icerik = (List<Data>)dataset_cache.Get("icerik");//134854 rows

List<Data> follow_icerik = icerik.Join(follow,
                i => i.Row.Where(w => w.NAME == "CrawlSourceId").Select(s => s.VALUE),
                f => f.Row.Where(w => w.NAME == "crawl_source_id").Select(s => s.VALUE),
                (i, f) =>
                    new Data
                    {
                        Row = i.Row.Concat(nf.Row).ToList()
                    }
                ).Take(5).ToList();

But it returns empty, how to use inner join when we have list in "on" clause?

表 1 数据图像

表 2 数据图像

Try this:

List<Data> follow_icerik = icerik.Concat(
                icerik.SelectMany(e => e.Row)
                                 .Where(w => w.NAME == "CrawlSourceId")
                                 .Join(follow.SelectMany(e => e.Row)
                                              .Where(w => w.NAME == "crawl_source_id"),
                i => i.VALUE,
                f => f.VALUE,
                (i, f) =>
                     new List<DataResultObject> { i, f }
                ).Select(e => new Data { Row = e })
).ToList();

EDIT:

icerik.SelectMany(e=>e.Row) - select rows with data which we are needed

icerik.SelectMany(e=>e.Row).Where(w => w.NAME == "CrawlSourceId") - filter this data

... Join(... - join filtered data

In Join we also have to filter data before join: follow.SelectMany(e=>e.Row).Where(w => w.NAME == "crawl_source_id")

i => i.VALUE, f => f.VALUE, - fields on which we join data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM