Foreach on IEnumerable or IList

I am using LINQ with an Entity base to join three tables, and I am returning an IList interface.

I want to apply a foreach for this return value so that I can populate the values ​​in my model.

This is how I join:

 public IList GetData(long Id) { //var q = dataContext.tblUsers.AsEnumerable().Join(dataContext.tblUsersProfiles.AsEnumerable(),) var query = from u in dataContext.tblUsers join p in dataContext.tblUsersProfiles on u.ProfileId equals p.Id join c in dataContext.tblComments on u.Id equals c.Commentedby where c.Id == Id select new { u.Firstname, u.Lastname, p.ProfilePicPath, c.Comment, c.CommentDate }; return query.ToList(); } 

I want to fill in these values ​​in my list of models. I tried this:

 var lst = repository.GetDate(Id); foreach (var item in lst) { } 

But I can not access the Firstname / Lastname , etc.

I also tried using item and object[] item objects, but both of them also do not work.

How can I apply for each on IList`?

This will work fine. If in this case I can return a DataTable instead of an IList .

+6
source share
6 answers

An anonymous type is created. Just define a class for the result of your request, then the return type of your function should look like this:

 public IList<YourClassName> GetData(long Id) { } 

Set the class values ​​as follows:

 select new YourClassName { Firstname = u.Firstname, Lastname = u.Lastname, ProfilePicPath = p.ProfilePicPath, Comment = c.Comment, CommentDate = c.CommentDate }; 
+7
source

In the method, you return an anonymous list of objects . You can access properties using reflection, but this is slow execution at runtime. It is easy to get information about a type - this is to define a class and use it in a select statement.

 public class Person { public string Firstname { get; set; } public string Lastname { get; set; } /* ... */ } public IList<Person> GetData(long Id) { var query = from u in dataContext.tblUsers join p in dataContext.tblUsersProfiles on u.ProfileId equals p.Id join c in dataContext.tblComments on u.Id equals c.Commentedby where c.Id == Id select new Person { u.Firstname, u.Lastname /* ... */ }; return query.ToList(); } 
+4
source

You create an anonymous type:

 select new { u.Firstname, u.Lastname, p.ProfilePicPath, c.Comment, c.CommentDate }; 

If you want to pass this data to other methods, etc., you need to create a real class.

 public class DataClass { public string Firstname { get; set; } public string LastName{ get; set; } public string ProfilePicPath { get; set; } public string Comment { get; set; } public DateTime CommentDate { get; set; } } 

and use it:

 public IList<DataClass> GetData(long Id) { var query = from u in dataContext.tblUsers join p in dataContext.tblUsersProfiles on u.ProfileId equals p.Id join c in dataContext.tblComments on u.Id equals c.Commentedby where c.Id == Id select new DataClass { Firstname = u.Firstname, Lastname = u.Lastname, ProfilePicPath = p.ProfilePicPath, Comment = c.Comment, CommentDate = c.CommentDate }; return query.ToList(); } 
+1
source

You should return not only the IList interface, but also a parameterized interface with your type. You are now returning the anounymouse object.

So what I want to offer. Create this class:

 public class MyCustomContainer { public string Firstname { get; set; } public string Lastname { get; set; } public string ProfilePicPath { get; set; } public string Comment { get; set; } public string CommentDate { get; set; } } 

And change your method as follows:

  public IList<MyCustomContainer> GetData(long Id) { //var q = dataContext.tblUsers.AsEnumerable().Join(dataContext.tblUsersProfiles.AsEnumerable(),) var query = from u in dataContext.tblUsers join p in dataContext.tblUsersProfiles on u.ProfileId equals p.Id join c in dataContext.tblComments on u.Id equals c.Commentedby where c.Id == Id select new MyCustomContainer { Firstname = u.Firstname, Lastname = u.Lastname, ProfilePicPath = p.ProfilePicPath, Comment = c.Comment, CommentDate = c.CommentDate }; return query.ToList(); } 

Now you return a non-anonymous object inside the list to get all the necessary properties

+1
source

You use an anonymous type to not throw it away no matter what when you iterate over your list. You can try using dynamic instead of var , which means you can call any property on it if it exists at run time:

 foreach (dynamic item in lst) { string firstName = item.FirstName; } 

or explicitly sets the class for the result element, like other answers.

0
source

You must change the return type to GetData strong>. Try the following:

 public List<tblUsers> GetData(long Id) { //var q = dataContext.tblUsers.AsEnumerable().Join(dataContext.tblUsersProfiles.AsEnumerable(),) var query = from u in dataContext.tblUsers join p in dataContext.tblUsersProfiles on u.ProfileId equals p.Id join c in dataContext.tblComments on u.Id equals c.Commentedby where c.Id == Id select new tblUsers { u.Firstname, u.Lastname, p.ProfilePicPath, c.Comment, c.CommentDate }; return query.ToList(); } 
0
source

All Articles