I want to join these two lists together with common key being the date but whatever I tried doesn’t work, any suggestions?
Classes:
public class item_mod
{
[JsonProperty(PropertyName = "item.price")]
public string price { get; set; }
[JsonProperty(PropertyName = "item.name")]
public string name { get; set; }
[JsonProperty(PropertyName = "@modify_stamp")]
public string stamp { get; set; }
[JsonProperty(PropertyName = "$trans")]
public string trans { get; set; }
[JsonProperty(PropertyName = "trans.date")]
public string date { get; set; }
}
public class trans_mod
{
[DataMember]
public string refer { get; set; }
[DataMember]
public string date { get; set; }
[DataMember]
public string time { get; set; }
[DataMember]
public string location { get; set; }
[DataMember]
public int points { get; set; }
[DataMember]
public string _total { get; set; }
[DataMember]
public string _discount { get; set; }
[JsonProperty(PropertyName = "$$position")]
public string position { get; set; }
[JsonProperty(PropertyName = "@modify_stamp")]
public string stamp { get; set; }
[JsonProperty(PropertyName = "$trans")]
public string trans { get; set; }
}
Lists: (I grouped them with Date being the key)
var q1 = it.GroupBy(x => x.date)
.Select(g => new
{
Date = g.Key,
Details = g.Select(x => new
{
Name = x.name,
Price = x.price,
Trans = x.trans,
})
.ToList()
})
.ToList();
var q2 = t.GroupBy(x => x.date)
.Select(g => new
{
Date = g.Key,
Details = g.Select(x => new
{
Location = x.location,
Points = x.points,
Pos = x.position,
Time = x.time,
Discount = x._discount,
Totals = x._total,
Trans = x.trans,
Ref = x.refer
})
.ToList()
});
I want to merge these two together in a group join - any suggestions how to?
What I've tried so far:
var query = from trans in q2
join item in q1 on q2 equals q1.Date into gj
select new { Date = q2.Date, Item = gj };
=> doesn't work returns blanks
var mergedList = q1.Union(q2).ToList();
=> also doesn't work.
Any help is appreciated, I'm also noting that I am very new to object oriented programming and I'm just trying to figure out how to do things!
CodePudding user response:
you can merge by SelectMany
var query = q2.SelectMany(x =>
q1.Where(z => z.Date == x.Date)
.Select(z => new
{
Date=z.Date,
q1Details = z.Details,
q2Details= x.Details
})
).ToList();
CodePudding user response:
var myvar = from a in it
join b in t on a.date equals b.date
select new { a.date, a.price, b.name, b.refer };
or:
var myvar = from a in it
join b in t on a.date equals b.date
select new { a.date, details= new {a.price, b.name, b.refer} };
or if you have multiple entries for same date
s and want to group by date
:
var myvar = from a in it
join b in t on a.date equals b.date
group new {a, b} by new {a.date} into g
select new { a.date, deatils = g.ToLİst() };