如何从不同的表中联合linq输出并返回

时间:2012-11-14 00:26:16

标签: c# asp.net-mvc linq

如何在两个linq结果之间执行联合并将结果返回到视图?

public ActionResult ShowAllStudents()
    {
        var StudentA = (from p in stud.Student_A.Where(a => a.StudentId != 0)
                                        group p by p.Index into g
                                        select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index).ToList();
        var StudentB = (from p in stud.Student_B.Where(a => a.StudentId != 0)
                                        group p by p.Index into g
                                        select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index).ToList();

        ViewData.Model = StudentA.Union(StudentB);


        return View();
    }

如果我尝试上面显示的方式,我会收到以下错误:

'System.collections.Generic.List<Student.Models.Student_A> does not contain a definition for 'Union' and the best extension method overload 'System.Linq.Queryable.Union<TSource>(System.Linq.IQueryable<TSource>, System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments

1 个答案:

答案 0 :(得分:0)

避免查询具体化。删除.ToList()调用。

<强>更新

   class Program
    {
        static void Main(string[] args)
        {
            var Student_A = new[]{
                new {StudentId = 1, Index = 23, Name = "Lucas"},
                new {StudentId = 2, Index = 71, Name = "Juan"},
                new {StudentId = 3, Index = 85, Name = "Noelia"}
            };
            var Student_B = new[]{
                new {StudentId = 6, Index = 31, Name = "Marcelo"},
                new {StudentId = 7, Index = 72, Name = "Manuel"},
                new {StudentId = 8, Index = 95, Name = "Roberto"}
            };

            var StudentA = (from p in Student_A.Where(a => a.StudentId != 0)
                            group p by p.Index into g
                            select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index);
            var StudentB = (from p in Student_B.Where(a => a.StudentId != 0)
                            group p by p.Index into g
                            select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index);

            var all = StudentA.Union(StudentB);

            all.ToList().ForEach(x=> Console.WriteLine(x.Name));
        }
    }

与此代码段有什么不同?

更新2: 好,我知道了。问题是Student_A的元素和Student_B的元素是不同的类型。我可以重现您的错误,如下所示

class Program
{
    static void Main(string[] args)
    {
        var Student_A = new[]{
            new {StudentId = 1, Index = 23, Name = "Lucas"},
            new {StudentId = 2, Index = 71, Name = "Juan"},
            new {StudentId = 3, Index = 85, Name = "Noelia"}
        };
        var Student_B = new[]{
            new {StudentId = 6, Index = 31, Name = "Marcelo", I=0},  // extra property
            new {StudentId = 7, Index = 72, Name = "Manuel", I=0},
            new {StudentId = 8, Index = 95, Name = "Roberto", I=0}
        };

        var StudentA = (from p in Student_A.Where(a => a.StudentId != 0)
                        group p by p.Index into g
                        select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index);
        var StudentB = (from p in Student_B.Where(a => a.StudentId != 0)
                        group p by p.Index into g
                        select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index)

        var all = StudentA.Union(StudentB);

        all.ToList().ForEach(x=> Console.WriteLine(x.Name));
    }

但是,如果我们使用相同的类型制作它们。看看下面的例子:

class Program
{
    static void Main(string[] args)
    {
        var Student_A = new[]{
            new {StudentId = 1, Index = 23, Name = "Lucas"},
            new {StudentId = 2, Index = 71, Name = "Juan"},
            new {StudentId = 3, Index = 85, Name = "Noelia"}
        };
        var Student_B = new[]{
            new {StudentId = 6, Index = 31, Name = "Marcelo", I=0},
            new {StudentId = 7, Index = 72, Name = "Manuel", I=0},
            new {StudentId = 8, Index = 95, Name = "Roberto", I=0}
        };

        var StudentA = (from p in Student_A.Where(a => a.StudentId != 0)
                        group p by p.Index into g
                        select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index)
                        .Select(x=> new {StudentId = x.StudentId, Index = x.Index, Name = x.Name });
        var StudentB = (from p in Student_B.Where(a => a.StudentId != 0)
                        group p by p.Index into g
                        select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index)
                        .Select(x=> new {StudentId = x.StudentId, Index = x.Index, Name = x.Name });

        var all = StudentA.Union(StudentB);

        all.ToList().ForEach(x=> Console.WriteLine(x.Name));
    }

区别在于:

。选择(x =&gt; new {StudentId = x.StudentId,Index = x.Index,Name = x.Name});