0

I am new to the MVC and I am stuck with a wierd situation. I have to read the Data from the type object and I tried different ways and I couldn't get a solution.Please help.

        IList<User> u = new UserRepository().Getuser(Name.ToUpper(), UserName.ToUpper(), UserCertNumber.ToUpper(), Date.ToUpper(), UserType.ToUpper(), Company.ToUpper(), PageNumber, Orderby, SearchALL.ToUpper(), PrintAllPages.ToUpper());


        object[] users = new object[u.Count];
        for (int i = 0; i < u.Count; i++)
        {
            users[i] = new
            {
                Id = u[i].UserId,
                Title = u[i].Title,
                FirstName = u[i].FirstName,
                LastName = u[i].LastName,
                Privileges = (from apps in u[i].UserPrivileges select new { PrivilegeId = apps.Privilege.PrivilegeId, PrivilegeName = apps.Privilege.Name, DeactiveDate = apps.DeactiveDate }),
                Status = (from status in u[i].UserStatus select new { StatusId = status.Status.StatusId, StatusName = status.Status.StatusName, DeactiveDate = status.DeactiveDate }),
                ActiveDate = u[i].ActiveDate,
                UserName = u[i].Email,
                UserCN = (from cert in u[i].UserCertificates select new { CertificateNumber = cert.CertificateNumber, DeactiveDate = cert.DeactiveDate }),
                Company = u[i].Company.Name

            };
        }



        string x = "";
        string y = "";

        var report = users;


        foreach (var r in report)
        {
            x = r[0].....;
            i want to assign the values from the report to something else and I am not able to read the data from the report object. Please help.
        }

Thank you.

4 Answers 4

1

Use the Select extension method so that you are directly creating the anonymous typed objects, rather than assigning them to a object of the generic Object class. You'll then be able to refer to the object's properties as desired.

   IList<User> us = new UserRepository().Getuser( Name.ToUpper(),
                                                  UserName.ToUpper(),
                                                  UserCertNumber.ToUpper(),
                                                  Date.ToUpper(),
                                                  UserType.ToUpper(),
                                                  Company.ToUpper(),
                                                  PageNumber,
                                                  Orderby,
                                                  SearchALL.ToUpper(),
                                                  PrintAllPages.ToUpper()); 

    var users = us.Select( u =>  new 
        { 
            Id = u[i].UserId, 
            Title = u[i].Title, 
            FirstName = u[i].FirstName, 
            LastName = u[i].LastName, 
            Privileges = (from apps in u[i].UserPrivileges select new { PrivilegeId = apps.Privilege.PrivilegeId, PrivilegeName = apps.Privilege.Name, DeactiveDate = apps.DeactiveDate }), 
            Status = (from status in u[i].UserStatus select new { StatusId = status.Status.StatusId, StatusName = status.Status.StatusName, DeactiveDate = status.DeactiveDate }), 
            ActiveDate = u[i].ActiveDate, 
            UserName = u[i].Email, 
            UserCN = (from cert in u[i].UserCertificates select new { CertificateNumber = cert.CertificateNumber, DeactiveDate = cert.DeactiveDate }), 
            Company = u[i].Company.Name 

        }); 


    string x = ""; 
    string y = ""; 

    var report = users; 

    foreach (var r in report) 
    { 
        var company = r.Company; // example
        ...
    }

EDIT: BTW, is there some reason why you are converting all those parameters to uppercase rather than simply doing case invariant comparisons in your repository?

Sign up to request clarification or add additional context in comments.

1 Comment

My Friend You are AWESOME.AWESOME..and AWESOME. It worked like a champ on the first shot. Thanks a TON :) :)
0

The way you do it, you create an anonymous type.

Create a class that contains those properties. Another way would be to put the report reading stuff inside the loop.

1 Comment

solution by tvanfosson worked for me. Thank you for your answer.
0
object o = new { Name = "string" };
Console.WriteLine(o.GetType().GetProperty("Name").GetValue(o, null));

But this is not really recommended practise. I would create standard data transfer class for this purpose.

1 Comment

solution by tvanfosson worked for me. Thank you for your answer.
0

Just Adding to tvanfosson solution :

If we want to get the PrivilegeName we do:

            foreach (var r in report) 
            { 
                x = r.FirstName; // example

                foreach (var s in r.Privileges)
                {
                    y = s.PrivilegeName; //Example
                }

            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.