简体   繁体   English

如何使用LINQ执行查询,该查询必须使一列与众不同但包括另一列的所有信息

[英]How to perform a query using LINQ that must make one column distinct but includes all information of another column

I am currently working on an assignment for class that deals with using LINQ to get information from a database filled with something like: 我目前正在为一个班级分配作业,该班级使用LINQ从数据库中获取信息,其中包含以下内容:

..StudentName..|..Class..|..GPA..|.....Course.....|..CourseNumber..|..FinalGrade..|..CSCIGPA.. ..StudentName .. | ..Class .. | ..GPA .. | ..... Course ........... CourseNumber .. | ..FinalGrade .. | ..CSCIGPA ..
......"D, Kara".....|....SR.... |...3.8...|...CSCI4220...|............4220.........|...........A.........|........3.9...... ......“ D,卡拉” ..... | .... SR .... | ... 3.8 ... | ... CSCI4220 ... | ....... ..... 4220 ......... | ........... A ......... | ........ 3.9 ... ...
......"D, Kara".....|....SR.....|...3.8...|...CSCI3110...|...........3110..........|..........A..........|........3.9...... ......“ D,卡拉” ..... | .... SR ..... | ... 3.8 ... | ... CSCI3110 ... | ...... ..... 3110 .......... | .......... A .......... | ........ 3.9 .. ....
...."R, Cisco".....|....JR.....|...3.1...|...CSCI3010...|...........3030..........|..........B..........|........3.4...... ....“ R,Cisco” ..... | .... JR ..... | ... 3.1 ... | ... CSCI3010 ... | ........ ... 3030 .......... | .......... B .......... | ........ 3.4 .... ..
...."R, Cisco".....|....JR.....|...3.1...|...CSCI2070...|...........2070..........|..........C..........|........3.4...... ....“ R,Cisco” ..... | .... JR ..... | ... 3.1 ... | ... CSCI2070 ... | ........ ... 2070 .......... | ........ C .......... | ........ 3.4 .... ..
...."R, Cisco".....|....JR.....|...3.1...|...CSCI3030...|...........3030..........|..........B..........|........3.4...... ....“ R,Cisco” ..... | .... JR ..... | ... 3.1 ... | ... CSCI3030 ... | ........ ... 3030 .......... | .......... B .......... | ........ 3.4 .... ..
...."A, Barry"......|....FR.....|...4.0...|...CSCI1010...|...........1010..........|..........A..........|........4.0...... ....“ A,Barry” ... | ..FR ..... | ... 4.0 ... | ... CSCI1010 ... | ....... .... 1010 .......... | .......... A .......... | ........ 4.0 ... ...
...."A, Barry"......|....FR.....|...4.0...|...CSCI2010...|...........2010..........|..........A..........|........4.0...... ....“ A,Barry” ... | .... FR ..... | ... 4.0 ... | ... CSCI2010 ... | ....... .... 2010 .......... | .......... A .......... | ........ 4.0 ... ...
...."Q, Oliver".....|....SO....|...2.7...|...CSCI2020...|...........2010..........|..........C..........|........2.8...... ....“ Q,Oliver” ..... | .... SO .... | ... 2.7 ... | ... CSCI2020 ... | ......... ..2010 .......... | ........ C .......... | ........ 2.8 ..... 。
...."Q, Oliver".....|....SO....|...2.7...|...CSCI2020...|...........2010..........|..........C..........|........2.8...... ....“ Q,Oliver” ..... | .... SO .... | ... 2.7 ... | ... CSCI2020 ... | ......... ..2010 .......... | ........ C .......... | ........ 2.8 ..... 。
...."Q, Oliver......|....SO....|...2.7...|...CSCI2030...|...........2030..........|..........C..........|........2.8...... ....“ Q,奥利弗... | .... SO .... | ... 2.7 ... | ... CSCI2030 ... | ......... ..2030 ........... | .......... C .......... | ........ 2.8 ..... 。
......"H, Roy"......|....SO....|...3.8...|...CSCI2020...|...........2020...........|..........A..........|........3.7...... ......“ H,罗伊” ... | .... SO .... | ... 3.8 ... | ... CSCI2020 ... | ...... ..... 2020 ........... | .......... A .......... | ........ 3.7。 .....

I haven't included all of the columns, but this is a good sample. 我没有包括所有的列,但这是一个很好的示例。 Given this input, the query should return a list of students for each class (FR, SO, JR, Senior) ordered by GPA(large to small) and then by CSCIGPA(large to small). 给定此输入,查询应返回按GPA(从大到小)顺序排列的每个班级(FR,SO,JR,高级)的学生列表,然后由CSCIGPA(从大到小)顺序排列。 Additionally, each student to be displayed should have a concatenated "Course" and "FinalGrade", and each concatenated course and final grade should also be included in the list. 此外,要显示的每个学生都应具有串联的“课程”和“最终成绩”,并且每个串联的课程和最终成绩也应包括在列表中。 For example, the record to display "A, Barry" would be: 例如,显示“ A,Barry”的记录为:

"A, Barry", FR, 4.0, CSCI1010A CSCI2010A, 4.0 “ A,Barry”,FR,4.0,CSCI1010A CSCI2010A,4.0

Note: the individual course numbers "1010 and 2010" form the column CourseNumber are not part of the output. 注意:“课程编号”列中的个别课程编号“ 1010和2010”不是输出的一部分。

My biggest problem is figuring out how to get the concatinated course and final grade into the single student line (when I did Distinct() it took off all of the different courses and grades except for the first one). 我最大的问题是弄清楚如何将简化的课程和最终成绩纳入单个学生行(当我执行Distinct()时,它除第一门课程外,还摘走了所有不同的课程和成绩)。 Here is some of the code that I wrote, but I'll admit it's not very good and probably not helpful to this problem since I've already described it. 这是我编写的一些代码,但是我会承认它不是很好,并且可能已经解决了这个问题,因为我已经描述了它。

//method to iterate over all students within student table public IEnumerable<Student> GetAllStudents() { var myquery = databaseEDM.Students .OrderBy(x => x.Class).ThenByDescending(x => x.GPA).ThenByDescending(x => x.CSCIGPA) .ToList().Distinct(); return myquery; }

I haven't ever really done anything with databases or queries before this assignment, so any help would really be appreciated. 在执行此任务之前,我从未真正对数据库或查询执行任何操作,因此,我们将不胜感激。 Please let me know if I need to give any more information to get a proper answer. 如果需要更多信息以获取正确答案,请告诉我。 Thanks. 谢谢。

Other Note: This is just a simple console application that gets information from a database to output. 其他说明:这只是一个简单的控制台应用程序,可从数据库获取信息并输出。

Here is your working example: 这是您的工作示例:

        public IEnumerable<Student> GetAllStudents()
        {
            var myquery = databaseEDM.Students
                .GroupBy(x => new { x.StudentName, x.Class })
                .Select(s => new
                {
                    StudentName = s.Key.StudentName,
                    Class = s.Key.Class,
                    GPA = s.Average(g => g.GPA),
                    Courses = string.Join(" ", s.Select(c => c.Course + c.FinalGrade)),
                    CSCIGPA = s.Average(cs => cs.CSCIGPA)
                })
                .OrderBy(x => x.Class)
                .ThenByDescending(x => x.GPA)
                .ThenByDescending(x => x.CSCIGPA)
                .ToList()
                .Distinct();
            return myquery;
        }

What I did? 我做了什么?

I grouped students by StudentName and Class . 我按StudentNameClass将学生分组。

Then I selected only properties which you requested in the example. 然后,我仅选择您在示例中请求的属性。

Then I made average GPA for each group because. 然后,由于每个组我平均GPA

Then I joined Course plus FinalGrade for that group because yo want in one value. 然后,我加入了该CourseFinalGrade ,因为您想要一个值。

And in the end, I created average CSCIGPA for the group. 最后,我为小组创建了平均CSCIGPA

I hope that I understood correctly your issue. 希望我能正确理解您的问题。 Your data are not best formatted but this query should help. 数据的格式不是最好的格式,但是此查询应该有所帮助。

Your code expect a class Student and you do not have a Student class in the code posted. 您的代码需要一个学生班,而您发布的代码中没有学生班。 In the code below I'm expecting results to be in a datatabe (which you can get from a dataadapter). 在下面的代码中,我期望结果在数据表中(您可以从数据适配器获得)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("StudentName", typeof(string));
            dt.Columns.Add("Class", typeof(string));
            dt.Columns.Add("GPA", typeof(decimal));
            dt.Columns.Add("Course", typeof(string));
            dt.Columns.Add("CourseNumber", typeof(int));
            dt.Columns.Add("FinalGrade", typeof(string));
            dt.Columns.Add("CSCIGPA", typeof(decimal));

            dt.Rows.Add(new object[] {"D, Kara","SR",3.8,"CSCI4220",4220,"A",3.9});
            dt.Rows.Add(new object[] {"D, Kara","SR",3.8,"CSCI3110",3110,"A",3.9});
            dt.Rows.Add(new object[] {"R, Cisco","JR",3.1,"CSCI3010",3030,"B",3.4});
            dt.Rows.Add(new object[] {"R, Cisco","JR",3.1,"CSCI2070",2070,"C",3.4});
            dt.Rows.Add(new object[] {"R, Cisco","JR",3.1,"CSCI3030",3030,"B",3.4});
            dt.Rows.Add(new object[] {"A, Barry","FR",4.0,"CSCI1010",1010,"A",4.0});
            dt.Rows.Add(new object[] {"A, Barry","FR",4.0,"CSCI2010",2010,"A",4.0});
            dt.Rows.Add(new object[] {"Q, Oliver","SO",2.7,"CSCI2020",2010,"C",2.8});
            dt.Rows.Add(new object[] {"Q, Oliver","SO",2.7,"CSCI2020",2010,"C",2.8});
            dt.Rows.Add(new object[] {"Q, Oliver","SO",2.7,"CSCI2030",2030,"C",2.8});
            dt.Rows.Add(new object[] { "H, Roy", "SO", 3.8, "CSCI2020", 2020, "A", 3.7 });

            var myquery = dt.AsEnumerable()
               .OrderBy(x => x.Field<string>("Class")).ThenByDescending(x => x.Field<decimal>("GPA")).ThenByDescending(x => x.Field<decimal>("CSCIGPA")).ToList();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM