C# 关于IEnumerable或IList的foreach

C# 关于IEnumerable或IList的foreach,c#,linq,C#,Linq,我使用带有实体框架的LINQ连接三个表,并返回IList接口 我想对这个返回值应用foreach循环,以便在模型中填充值 我就是这样加入的: public IList GetData(long Id) { //var q = dataContext.tblUsers.AsEnumerable().Join(dataContext.tblUsersProfiles.AsEnumerable(),) var query = from u in dataContext.tblUsers

我使用带有实体框架的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

我还尝试使用object
item
object[]item
,但它们都不起作用

如何在IList`上为的每个
应用


在本例中,如果我能够返回
DataTable
,而不是
IList
,则可以正常工作。

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

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();
}
public IList GetData(长Id)
{
var query=来自dataContext.tblUsers中的u
在u.ProfileId等于p.Id的dataContext.tbluserProfiles中加入p
在u.Id上的dataContext.tblComments中加入c。等于c.Commentedby
其中c.Id==Id
选择新的数据类
{
名字=美国名字,
Lastname=u.Lastname,
ProfilePicPath=p.ProfilePicPath,
注释,
CommentDate=c.CommentDate
};
返回query.ToList();
}

您正在创建一个匿名类型。只需为查询结果定义一个类,那么函数的返回类型应该如下所示:

public IList<YourClassName> GetData(long Id)
{

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

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

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

或者像其他答案所建议的那样为结果项显式定义一个类。

您必须更改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();
        }
public List GetData(长Id)
{
//var q=dataContext.tblUsers.AsEnumerable().Join(dataContext.tbluserprofiles.AsEnumerable(),)
var query=来自dataContext.tblUsers中的u
在u.ProfileId等于p.Id的dataContext.tbluserProfiles中加入p
在u.Id上的dataContext.tblComments中加入c。等于c.Commentedby
其中c.Id==Id
选择新的tblUsers{u.Firstname,u.Lastname,p.ProfilePicPath,c.Comment,c.CommentDate};
返回query.ToList();
}

您不仅应该返回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<YourClassName> GetData(long Id)
{

}
  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();
        }
public IList GetData(长Id)
{
//var q=dataContext.tblUsers.AsEnumerable().Join(dataContext.tbluserprofiles.AsEnumerable(),)
var query=来自dataContext.tblUsers中的u
在u.ProfileId等于p.Id的dataContext.tbluserProfiles中加入p
在u.Id上的dataContext.tblComments中加入c。等于c.Commentedby
其中c.Id==Id
选择新建MyCustomContainer
{ 
名字=美国名字,
Lastname=u.Lastname,
ProfilePicPath=p.ProfilePicPath,
注释,
CommentDate=c.CommentDate
};
返回query.ToList();
}

现在,您正在返回列表中的非匿名对象,以便可以获得所需的所有属性

您正在方法中返回
对象的
匿名
列表。您可以通过使用反射来访问属性,但在运行时这有点慢。获取类型信息最简单的方法是定义一个类,并在
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();
}
公共类人物
{
公共字符串名{get;set;}
公共字符串Lastname{get;set;}
/* ... */
}
公共IList GetData(长Id)
{   
var query=来自dataContext.tblUsers中的u
在u.ProfileId等于p.Id的dataContext.tbluserProfiles中加入p
在u.Id上的dataContext.tblComments中加入c。等于c.Commentedby
其中c.Id==Id
选择新人物{u.Firstname,u.Lastname/*…*/};
返回query.ToList();
}