Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, November 27, 2017 7:14 AM
I have a LINQ of
var holeinfo = (from h in context.HOLELOCATIONs
from d in context.DRILLHOLEDIAMDETAILS
where h.HOLEID == holeId && h.HOLEID == d.HOLEID && d.NAME == "rig_id"
select new
{
holeid = h.HOLEID,
startdate = h.STARTDATE,
enddate = h.ENDDATE,
depth = h.DEPTH,
rigid = d.VALUE
})
This return data like
"AAA", 2017-11-01, 2017-11-02, 25, "RIG 1"
"AAA", 2017-11-01, 2017-11-02, 25, "RIG 2"
I would like to return "AAA", , 2017-11-01, 2017-11-02, 25, "RIG 1 | Rig 2" but I am having major issues getting it to group and String.Join(" | ", rigid)
Thanks
All replies (2)
Monday, November 27, 2017 9:05 AM âś…Answered
Hi Quentin.Samuelson,
According to your description, I create a simple, which use LINQ gourp and c# method named string.Join as below for your reference.
string holeId = "AAA";
using (var context = new EFDemoContext())
{
var holeinfo = (from h in context.HOLELOCATIONs
from d in context.DRILLHOLEDIAMDETAILs
where h.HOLEID == holeId && h.HOLEID == d.HOLEID && d.NAME == "rig_id"
select new
{
holeid = h.HOLEID,
startdate = h.STARTDATE,
enddate = h.ENDDATE,
depth = h.DEPTH,
rigid = d.VALUE
});
var result = holeinfo.ToList().GroupBy(t => new { t.holeid, t.startdate, t.enddate, t.depth }).Select(t => new
{
holeid = t.Key.holeid,
startdate = t.Key.startdate,
enddate = t.Key.enddate,
depth = t.Key.depth,
rigid = string.Join("|",t.Select(x=>x.rigid))
}).ToList();
Best regards,
Zhanglong Wu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Monday, November 27, 2017 10:57 PM
Thanks Zhanglong,
I needed only one copy of the String so I added a Distinct.
string.Join("|", t.Select(x => x.rigid).Distinct())}).ToList();
Regards
Quentin