TheMonkeyMan
TheMonkeyMan

Reputation: 9152

Linq Sub-select Query

I have a collection like this

    public class Worksheet
    {
        public string WorksheetName { get; set; }
        public string WorksheetId { get; set; }
        public string TestSymbol { get; set; }
        public string TestId { get; set; }

        public List<Samples> Samples { get; set; }

        public Worksheet()
        {
            Samples = new List<Samples>();
        }
        public Worksheet(string worksheetname, string worksheetid, string testsymbol, string testid)
        {
            WorksheetName = worksheetname;
            WorksheetId = worksheetid;

            TestSymbol = testsymbol;
            TestId = testid;

            Samples = new List<Samples>();
        }

    }
    public class Samples
    {
        public string SampleId { get; set; }
        public string SampleName { get; set; }

        public Samples()
        {
            AssociatedSheets = new List<Worksheet>();
        }

        public Samples(string sampleid, string samplename)
        {
            AssociatedSheets = new List<Worksheet>();

            SampleId = sampleid;
            SampleName = samplename;
        }

        public List<Worksheet> AssociatedSheets { get; set; }
    }

The structure when viewed in a watch appears

What I want is to set the AssociatedSheets in each sample to display the Worksheet that contains it.

So that it displays

I have developed something like this but obviously it wont work. After this I want to distinct select each sample. Can someone advise:

        List<Samples> AllSamples = new List<Samples>();

        //Step 4. Add possible worksheets to each sample.
        foreach (Samples sample in sheets.SelectMany(x => x.Samples).ToList())
        {
            List<Worksheet> sampleitem = sheets.SelectMany(x => x.Samples.Select(y => y.SampleId) == sheets.SelectMany(k => k.Samples).Select(h => h.SampleId).ToList());

            sample.AssociatedSheets = AssociatedSheets;

            if (!AllSamples.Contains(sample))
                AllSamples.Add(sample);
        }

Upvotes: 0

Views: 609

Answers (2)

C&#233;dric Bignon
C&#233;dric Bignon

Reputation: 13022

Simple but do the job:

foreach (var grouping in from worksheet in worksheets
                         from sample in worksheet.Samples
                         group worksheet by sample)  // Groups the worksheets by sample
    grouping.Key.AssociatedSheets = grouping.Distinct().ToList();  // grouping.Key is the key of the group (the sample) and ((IEnumerable)grouping) is the elements in the group (the worksheets

Upvotes: 1

polkduran
polkduran

Reputation: 2551

[Edit] code fix from my former answer

maybe this can do it :

List<Sample> allSamples = new List<Sample>();
foreach (Sample sample in sheets.SelectMany(x => x.Samples).Distinct())
{
  var sampleSheets = sheets.Where(s => s.Samples.Contains(sample)).Distinct();
  sample.AssociatedSheets.AddRange(sampleSheets);
  allSamples.Add(sample);
}

Upvotes: 1

Related Questions