简体   繁体   中英

Reading DATA from an OBJECT asp.net MVC C#

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.

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?

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.

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.

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
                }

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM