3

我有一个类似这样的 linq 查询

var Query = from c in table
            where (some condition)
            select new {
            Name = c.Name,
            courses = // this returns a list,
            };

我如何将它绑定到gridview,以便结果是这样的

name1 course1
name1 course2
name1 course3
name2 course1
name2 course2

有任何想法吗?

4

1 回答 1

2

试试下面

gridview.DataSource = Query.ToList().Select(a => a.courses
             .Select(c => new { Name = a.Name, Course = c }))
             .SelectMany(p=>p).ToList();
gridview.DataBind();

如果你想从方法中返回这个列表,那么创建如下的类

public class MyClass
{
    public string Name { get; set; }
    public string Course { get; set; }
}

现在您可以将列表返回为

public List<MyClass> MyMethod()
{
    var Query = from c in table
                where (some condition)
                select new {
                Name = c.Name,
                courses = // this returns a list,
                };

    return Query.ToList().Select(a => a.courses
            .Select(c => new MyClass{ Name = a.Name, Course = c }))
            .SelectMany(p=>p).ToList();
}
于 2013-05-03T06:21:26.500 回答