简体   繁体   中英

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

  • Worksheet 1
    • Sample A
    • Sample B
  • Worksheet 2
    • Sample C
    • Sample A

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

So that it displays

  • Worksheet 1
    • Sample A
      • Worksheet 1
      • Worksheet 2
    • Sample B
      • Worksheet 1
  • Worksheet 2
    • Sample C
      • Worksheet 2
    • Sample A
      • Worksheet 1
      • Worksheet 2

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);
        }

[ 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);
}

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

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