Click here to Skip to main content
15,113,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have problem with Linq To Entity. I have 2 table called Member and Payment. I use Linq code to retrieve the data. Here's the code:
Array array = (from member in container.Member
		join payment in container.Payment
		on member.MemberCode equals payment.MemberCode
		select new { member.Name, member.Rank, payment.DatePaid, payment.Remarks }).ToArray();


My question is: how to get each element of this array? I want to add each element into ListView.
It's easy to set array as DataGridView data source by writing dataGridView.DataSource = list;
All I want here is to get each element of this array because I need to loop them and insert them into ListView.
I can't access it with this line because this array contains both Member and Payment.
C#
string nm = string.Empty;
string dp = string.Empty;
foreach(Member m in array)
{
    nm = m.Name; 
    dp = m.DatePaid //It can't
}
Posted

1) You can not cast implicitly between well-known and anonymous classes.
2) The array you construct is not an array of Member
Here are your options:
Method 1)
Define a class that contains all four members you want to return as result, and select the result as enumeration of that class (...select new classname { Name=Memeber.name, .... and foreach(classname m in array)...)
Method 2)
You can use reflection to read the member values. This is an overhead, thus be careful.
Method 3)
Declare the array also as anonymous (var array=(from... and foreach(var m in array)...)
Method 4)
Use the new dynamic supertype (foreach(dynamic m in array)...)

I would rather use method 1.

PS: You should convert it to array only when really needed, since you miss the advantages of deferred execution.
   
Comments
derodevil 10-Aug-12 13:33pm
   
Thank you for the solution. I'll try it.
The reason you can't is because you are using an anonymous type.
Try this instead
C#
var array = (from member ...
foreach(var t in array){
}

Then using the debugger you can check what members t has. Probably name, rank and datepaid and remarks
   
v2
Comments
derodevil 8-Aug-12 12:58pm
   
Yes, you're right. And how to get the values?
this code still generates errors
string nm = string.Empty;
string dp = string.Empty;
foreach(var t in array)
{
nm = t.Name;
dp = t.DatePaid;
}
Philip Stuyck 8-Aug-12 13:18pm
   
well, if DatePaid is a DateTime structure, then assigning it on a string won't work. either change dp to DateTime or use dp=t.DatePaid.toString().
Please mark this as a solution to give me credit for it.
If this is not the problem then tell me the compilaton error and the line where it complains about.
derodevil 8-Aug-12 13:22pm
   
I still can't access the property values. t.Name error, t has no property named Name and DatePaid.
Philip Stuyck 8-Aug-12 13:47pm
   
I told you to use a debugger to find out the proper names but if you do this :
select new {Name= member.Name,Rank= member.Rank,DatePaid= payment.DatePaid,...
then you are forcing the anonymous type members to have member names defined by yourself.
Philip Stuyck 8-Aug-12 14:15pm
   
Also take the PS of Zoltan into account. You don't need toArray(). It works perfectly well without it too.
derodevil 10-Aug-12 13:34pm
   
Ok, thank you philip for answering my question.
derodevil 10-Aug-12 13:34pm
   
Thank you philip for answering my question. Thanks a lot.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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