views:

45

answers:

4

I have 2 IEnumerable of Type1 and Type2, ie. IEnumerable<Type1> & IEnumerable<Type2>. Type1 and Type2 have 1 common field, called TypeID. Multiple of same TypeID may exists in IEnumerable<Type2>, I would like to check against the 2 IEnumerables and if TypeID inside Type2 equals to TypeID inside Type1, I would combine the 2 into a new object. If they are not, then the new object just set IEnumerable<Type2> as null. It's a bit confusing, so I've include a pseudo code to better demonstrate what I'm trying to accomplish:

namespace test
{
    public class ConfigMetaDataColumns
    {
        public int FieldID { get { return ValueInt("FieldID"); } }
        public string Label { get { return ValueString("Label"); } }
        public string FieldName { get { return ValueString("FieldName"); } }
    }

    public class ConfigDataColumns
    {
        public int FieldID { get { return ValueInt("FieldID"); } }
        public double NumericValue { get { return ValueDouble("NumericValue"); } }
    }

    public class ConfigMetaDataCombinedColumns
    {
        public ConfigMetaDataColumns ConfigMetaData { get; set; }
        public IEnumerable<ConfigDataColumns> ConfigData { get; set; }
    }

    public class GetCombinedData
    {
        // Get ConfigMetaData
        private IEnumerable<ConfigMetaDataColumns> GetConfigMetaData()
        {
            var requester = GetConfigMetaDataMethodHere();
            return requester.Items;
        }

        // Get ConfigMeta
        private IEnumerable<ConfigMetaDataColumns> GetConfigMetaData()
        {
            var requester = GetConfigDataMethodHere();
            return requester.Items;
        }

        // Combine the two here!
        private IEnumerable<ConfigMetaDataCombinedColumns> GetData()
        {
            /*
             * ConfigMetaDataColumns example:
             * FieldID:     1
             * Label:       Label1
             * FieldName:   FieldName1
             * 
             * FieldID:     2
             * Label:       Label2
             * FieldName:   FieldName2
             * 
             * FieldID:     3
             * Label:       Label3
             * FieldName:   FieldName3
             * */

            /*
             * ConfigDataColumns example:
             * FieldID:     1
             * NumericVal:  NumericVal1

             * FieldID:     1
             * NumericVal:  NumericVal2

             * FieldID:     3
             * NumericVal:  NumericVal3
             * */

            /*
             * Cobined data should be:
             * 
             * FieldID:     1
             * Label:       Label1
             * FieldName:   FieldName1
             {* FieldID:    1
             * NumericVal:  NumericVal1
             * FieldID:     1
             * NumericVal:  NumericVal2}
             * 
             * FieldID:     2
             * Label:       Label2
             * FieldName:   FieldName2
             {* NULL *}
             * 
             * FieldID:     3
             * Label:       Label3
             * FieldName:   FieldName3
             {* FieldID:    3
             * NumericVal:  NumericVal3}
             * */
        }
    }
}

Thank you.

A: 

Complete guess... but I think this is what you want:

private IEnumerable<ConfigMetaDataCombinedColumns> ReturnMe()
{
   var query = from meta in GetConfigMetaData().AsQueryable()
            join data in GetConfigDataColumns().AsQueryable()
            on meta.FieldID equals data.FieldID
            select new {
                ConfigMetaDataColumns = meta,
                ConfigDataColumns = data
            };
   return  from item in query
            group item by item.ConfigMetaDataColumns into l 
            select new ConfigMetaDataCombinedColumns()
            {
                ConfigMetaData = l.Key,
                ConfigData = l.Select(x=>x.ConfigDataColumns)

            }        ;
}
Nix
A: 

I think this should do it. Not the most efficient, but you can improve it storing the result from your data fetching functions in variables:

IEnumerable<ConfigMetaDataCombinedColumns> combined =
    GetConfigMetaData()
    .Select(p=> new ConfigMetaDataCombinedColumns() {ConfigMetaData = p
        , ConfigData = GetConfigData().Where(q=>q.FieldID == p.FieldID)  });
Francisco
A: 

I believe a Join with the ResultSelector calling your GetData method, but passing the objects as arguments, will work for you.

//IEnumerable<Type1> List1
//IEnumerable<Type2> List2

var result = List1.Join( List2, a => a.TypeId, b => b.TypeId, (a, b) => GetData(a, b));
//do something with result
Sorax
+2  A: 

If I read your question correctly, all you need is essentially a left join.

Example:

class Foo
{
    public int SomeId { get; set; }
}

class Bar
{
    public int SomeId { get; set; }
}

class FooWithBars
{
    public Foo Foo { get; set; }
    public IEnumerable<Bar> Bars { get; set; }
}

...

List<Foo> foos = new List<Foo>();
List<Bar> bars = new List<Bar>();

foos.Add(new Foo() { SomeId = 1 });
foos.Add(new Foo() { SomeId = 2 });

bars.Add(new Bar() { SomeId = 1 });
bars.Add(new Bar() { SomeId = 1 });

// get all foos and matching bars
var combined = from foo in foos
                join bar in bars
                on foo.SomeId equals bar.SomeId into g
                select new FooWithBars
                {
                    Foo = foo,
                    Bars = g.Any() ? g : null
                };

In this example, you get an IEnumerable<Bar> or null, as per your requirement.

Anthony Pegram
Thanks Anthony, this works!
Saxman