Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
foreach (object New in data1;object old in data2)
{

}

Is it possible?
I need to fetch two objects from a database and append both of them to another one
Posted
Comments
BillWoodruff 5-Dec-14 5:26am    
In this case, it's very important to know exactly what "append" means. If your 'data1 and 'data2 are generic Lists of Type 'string, that's one thing; if they are collections of instances of two different Classes whose structure is not the same: that's another thing.

Looked at one way, "append" could mean "produce a new structure that holds two objects;" looked at another way, "append" could mean merge/join/sum ... and, so on.

"Append" in the case of two indexable collections could mean select from both using an ordinal position; or, "append" could mean select from both collections based on some dynamic procedure/query/regex/Linq-filter, etc.

So, please clarify.

Not directly but you can try with LINQ :
C#
foreach( var both in listA.Zip(listB, (a,b)=> new { a, b}))
    {
          both.a.Print();
          both.b.Print();
    }


Sample code:
void Main()
{
	List<A> listA = new List<A>(){ new A("aaa1"), new A("aaa2")};
	List<B> listB = new List<B>(){ new B("bbb1"), new B("bbb3")};
	foreach( var both in listA.Zip(listB, (a,b)=> new { a, b}))
	{
	      both.a.Print();
		  both.b.Print();
	}
}

public class A
{
    public string Val{ get; set;}
     public A( string a)
	{ 
	  Val =a;
	}
    public void Print()
	{
	   Console.WriteLine( "Class A:"+Val);
	}
}

public class B
{
    public string Val{ get; set;}
    public B( string b)
	{ 
	   Val =b;
	}
	public void Print()
	{
	   Console.WriteLine( "Class B:"+Val);
	}
}
 
Share this answer
 
v4
Comments
BillWoodruff 5-Dec-14 5:30am    
+5 This is very cool use of Linq you demonstrate, and it is a reasonable assumption that the OP wants to join two collections whose objects are of identical Types, but ... in this case ... we don't know ... yet ... exactly what is in the two "collections" which will indicate what "append" actually means.
MAGuru 5-Dec-14 23:33pm    
thank you for your reply,But i need to assign two objects into two another diffrent objects
DamithSL 5-Dec-14 23:40pm    
you haven't explain your problem in detail. we can't read your mind. please elaborate on what you mean by "two objects" here and "another diffrent objects"
BillWoodruff 6-Dec-14 2:27am    
You can't say we haven't tried to assist you !
No. But there are LINQ alternatives which you can use such as JOIN or ZIP method which will allow you to combine two collections into one. Then you can iterate over combined collection to get the desired value.
 
Share this answer
 
Comments
BillWoodruff 5-Dec-14 5:27am    
Yes, but in this case we don't know ... yet ... what is in the two "collections."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900