locked
linq inner join extension for multiple lists (Generic way) RRS feed

  • Question

  • User-479818971 posted

    Hi ,

    I want to know about is there any way to do Joining multiple list using Linq (Generic way).

    I just create a generic method for joining 2 List

    public static T Merge<T>(T List1, T List2) where T : new()
    		{
    			var properties = typeof(T)
    				.GetProperties()
    				.Where(p => p.CanRead && p.CanWrite && !p.GetIndexParameters().Any());
    
    			T result = new T();
    
    			foreach (var prop in properties)
    			{
    				object value = prop.GetValue(List1) ?? prop.GetValue(List2);
    				prop.SetValue(result, value);
    			}
    
    			return result;
    		}
    List<student> _List1 = List1.Cast<student>().ToList();
    List<student> _List2 = List2.Cast<student>().ToList();
    var mergedlist = from a in _List1
    	join b in _List2
    	on a.stid equals b.stid 
    	select Merge(a, b);
    

    But i need to pass multiple List of List into Merge function .How we can achieve ?

    How we can do using TModel ?

    Saturday, June 13, 2020 8:37 AM

All replies

  • User303363814 posted

    select Merge(Merge(a,b),c)

    would do three lists

    select Merge(Merge(Merge(a,b),c,d) -or- select Merge(Merge(a,b),Merge(c,d) 

    would do four lists.  I'm sure you can guess how to do more.

    Sunday, June 14, 2020 12:43 AM