I am new to the MVC and I am stuck with a wierd situation. I have to read the Data from th开发者_Go百科e 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
}
}
精彩评论