1

如何在两个 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
4

1 回答 1

1

Avoid the queries materialization. Remove the .ToList() calls.

Update:

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

Which is the diference with this snippet?

Update 2: Ok, I got it. The problem is Student_A´s elements and Student_B's elements are different types. I could reproduce the error you have as follow

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

However, if we make both with the same type it works. Take a look at the follow example:

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

The difference is in the line:

.Select(x=> new {StudentId = x.StudentId, Index = x.Index, Name = x.Name });

于 2012-11-14T00:34:52.887 回答