Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
dynamic firstquery = null;
C#
firstquery = from FeeCollection in context.tbl_FeeCollection
             select new
                              {
                                  FeeCollection.Createddatetime,
                                  FeeCollection.StudentID
                               };



How can I access StudentID and Createddatetime?I have tried following code
foreach(var item in firstquery)
{
item.StudentID // here error
}
Posted
Comments
Pheonyx 17-Sep-14 4:42am    
What is the error you are getting?
johannesnestler 17-Sep-14 5:48am    
Normally on QA this question makes sense, but in this case you should have read the post more carefully. It's about how to access anonymouse types and not an "error"...

Hi,
So you ask "How to Access the propterties of anonymouse types"?
It has nothing to do with EntityFramework, Asp.NET, or the dynamic keyword. - Next time maybe tag your question better (in this case I think you are missing basic knowledge about .NET so a day with a good book could help more than 1000 questions on the Internet....)

So give the docu a read:

[^]

In short (cause I know that no one who is told to read the manual/docu does it...): Use var and not dynamic (complete nonsense in this case) and your code will work.

Kind Regards,

Johannes

P.S. I'm answering questions here on CP because I READ THE DOCUMENTATION - Please don't take this joking as offense - I just want to point you in the right direction. MSDN is a great source for knowledge, if you know how to use it (hit F1 on any keyword, or function in VS)
 
Share this answer
 
Comments
m-shraddha 17-Sep-14 11:23am    
I have already read the link which you have provided. But then too thanks a lot. I know how and when to use var and how it is useful. But as I am using dynamic datatype, I want to know how to retrieve data with the help of dynamic variable that has been used. I am using keyword dynamic because I am using "query" with multiple conditions and want to store all the queries in single "query". From multiple conditions and queries , only single query will be fired at the end of the execution. So instead of creating different var query1,queyr=2,query3 , I have created a single "query" and stored different queries according to conditions into a single query which can be only done by keyword "dynamic".
johannesnestler 18-Sep-14 3:20am    
I'm not shure if I 100% understand what you want, but if you do it like this you would be better building an dynamic Expression and store that. Anyway if you really have to, you can always go for reflection (just reflect on the item) - but I'd have a look at http://www.codeproject.com/Articles/402594/Black-Art-LINQ-expressions-reuse first...
m-shraddha 18-Sep-14 5:53am    
I got the solution. Hence thanks for suggestions
johannesnestler 18-Sep-14 8:13am    
So you go for reflection? Just keeep an eye on the performance. Good luck with your project!
m-shraddha 19-Sep-14 0:01am    
yeah sure..thanks
use..
C#
 var firstquery = from FeeCollection in context.tbl_FeeCollection
              select new
                               {
                                   time=FeeCollection.Createddatetime,
                                  id= FeeCollection.StudentID
                                };

foreach(var x in firstquery )
{
string Id=x.id;
string time=x.time
}
 
Share this answer
 
you need to add a check before your loop to confirm the the collection not null or empty

C#
if(firstquery != null && firstquery.count >0)
{
foreach(var item in firstquery)
{
item.StudentID // here error
}
}
 
Share this answer
 
Well, you get the error because your anonymous type that you defined in your select doesn't define a StudentId field. Your code should be:
C#
dynamic firstquery = from FeeCollection in context.tbl_FeeCollection
                     select new
                     {
                         CreationDateTime = FeeCollection.Createddatetime,
                         StudentId = FeeCollection.StudentID
                     };
 
Share this answer
 
v3
C#
foreach (dynamic item in firstquery)
                {
                    DataRow dr = NewTable.NewRow();
                    object obj = item;
                    Type temp = obj.GetType();
                    DateTime cdt =(DateTime)temp.GetProperty("cdt").GetValue(obj,null);
                    int studid = (int)temp.GetProperty("studid").GetValue(obj, null);
                   
                }
 
Share this answer
 

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