Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For example I have these objects.
<object1>
 <list1>
  <info>
  	<codedetails>A</code1>
  </info>
  <info>
  	<codedetails>B</code1>
  </info>
 </list1>
</object1>

<object2>
  <lst>
    <reference>1</reference>
	<info>
	       <code1>X</code1>
	</info>
	<info>
	       <code1>D</code1>
	</info>
  </lst>
  <lst>
    <reference>2</reference>
         <info>
		<code1>A</code1>
	</info>
	 <info>
		<code1>A</code1>
	</info>
  </lst>
    <lst>
    <reference>3</reference>
	<info>
		<code1>A</code1>
	</info>
	<info>
		<code1>B</code1>
	</info>
  </lst>
</object2>


In this case I need get the reference 3 because the two code1 agree ( A -> A, B-> B).
I try
var result = (from p in object1.list1
			   from f in object2.lst
			   where f.info.code1  == p.info.code1
			   select f.reference).FirstOrDefault();

The result of last code is the reference 2 but it will be the reference 3.
Can I the result of the reference 3 with LINQ?.
In this case I have two code ( A and B ) to compare, but I will have more.
Thank you very much.
Posted
Updated 25-Aug-14 23:00pm
v3
Comments
Kumarbs 26-Aug-14 3:29am    
In this case, the list properties are same names. Hence if you wrote a query the first property satisfies the condition then it comes in the result. So please try to change the property names.
Adrián Cejudo 26-Aug-14 3:42am    
Yes.
For example I can change the property names of object1. Codedetails instead code1.
thanks for your comment.
[no name] 26-Aug-14 9:02am    
not clear yet?
Adrián Cejudo 27-Aug-14 2:59am    
No...
I can't get the reference 3.
It returns the reference 2.

1 solution

you are using FirstOrDefault() to get the first result only. That's why it's giving only reference 2 not other.
if you need first result only then use
var result = (from p in object1.list1
			   from f in object2.lst
			   where f.info.code1  == p.info.code1
			   select f.reference).FirstOrDefault();

If you need both Result then use
var result = (from p in object1.list1
			   from f in object2.lst
			   where f.info.code1  == p.info.code1
			   select f.reference).ToList();

If you need last result only then use
var result = (from p in object1.list1
			   from f in object2.lst
			   where f.info.code1  == p.info.code1
			   select f.reference).LastOrDefault();
 
Share this answer
 
v2
Comments
Adrián Cejudo 27-Aug-14 4:05am    
Yes.
But int this example I don't need get the reference 2, only the 3.
In this example If I use .LastOrDefault It rules.
But in other object, for example:
<object2>
<lst>
<reference>2</reference>
<info>
<code1>A</code1>
</info>
<info>
<code1>B</code1>
</info>
</lst>
<lst>
<reference>3</reference>
<info>
<code1>A</code1>
</info>
<info>
<code1>A</code1>
</info>
</lst>
</object2>
It doesn't rule because the result would be the reference 3 and It must be the reference 2 because codedetails A = code1 A and codedetals B = code1 B
Reference 2
A = A
B = B
I going to use toList() and after a foreach to compare the values.
Thank you very much Sankarsan.
[no name] 27-Aug-14 4:19am    
you most welcome.
You can put condition wise selection query

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