在IEnumerable或IList上使用foreach

5
我正在使用带有 Entity framework 的 LINQ 来连接三个表,并返回 IList 接口。
我希望能够在返回的值上应用 foreach 循环,以便可以填充我的模型中的值。
这是我如何连接它们的:
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();
}

我希望能将这些值填充到我的模型列表中。我尝试了以下代码:
var lst = repository.GetDate(Id);
foreach (var item in lst)
{

}

但是我无法访问项目 Firstname/Lastname 等。

我还尝试使用对象 itemobject[] item,但它们都不起作用。

如何在 IList` 上应用 foreach呢?

如果能够在这种情况下返回 DataTable 而不是 IList,那么它将正常工作。

6个回答

7

您正在创建匿名类型。只需为查询结果定义一个类,然后函数的返回类型应为:

public IList<YourClassName> GetData(long Id)
{

}

最后设置类的值:
select new YourClassName 
{
    Firstname = u.Firstname,
    Lastname = u.Lastname, 
    ProfilePicPath = p.ProfilePicPath,
    Comment = c.Comment,
    CommentDate = c.CommentDate
};

4

该方法返回一个匿名的对象列表。您可以使用反射访问属性,但这会在运行时稍微慢一些。最简单的获取类型信息的方法是定义一个类,并在select语句中使用它。

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();
}

1
您正在创建一个匿名类型:

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

如果你想将这些数据传递给其他方法等,你需要创建一个真正的类。
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; }
}

并使用它:

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
你应该返回带有你的类型参数化的接口,而不仅仅是IList接口。现在你正在返回匿名对象。
所以我想建议创建这个类:
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; }
}

把你的方法改成这样:
  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();
        }

现在你返回的不是匿名对象,而是List中的对象,因此你可以获取所有需要的属性。

0

你正在使用匿名类型,因此在循环遍历列表时无法将其转换为任何内容。你可以尝试使用 dynamic 代替 var,这意味着只要它在运行时存在,你就可以调用它的任何属性:

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

或者像其他答案建议的那样,明确地为结果项定义一个类。


0

您必须更改GetData的返回类型。请尝试以下方法:

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();
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接