C# 使用linq读取Excel工作表-跳过重复id

C# 使用linq读取Excel工作表-跳过重复id,c#,excel,linq,linq-to-excel,C#,Excel,Linq,Linq To Excel,在下面的excel文件中,我需要以Student类的形式读取行,这些行定义了唯一ID的单个对象,然后获取与该学生关联的所有考试 如果我使用以下代码,我生成的学生数等于行数,并且没有为每个对象学生指定考试列表 IQueryable<Student> Students_var; var excel = new ExcelQueryFactory(fileName_global1); excel.AddMapping<Student>(x => x.ID, "STU

在下面的excel文件中,我需要以
Student
类的形式读取行,这些行定义了唯一ID的单个对象,然后获取与该学生关联的所有考试

如果我使用以下代码,我生成的学生数等于行数,并且没有为每个对象学生指定考试列表

IQueryable<Student> Students_var;

var  excel = new ExcelQueryFactory(fileName_global1);
excel.AddMapping<Student>(x => x.ID, "STU_NO");
Students_var = from c in excel.Worksheet<Student>("Stu_Schedule")
                select c;
List<Student> StudentList_c = Students_var.ToList();
IQueryable学生;
var excel=新的ExcelQueryFactory(文件名为\u global1);
AddMapping(x=>x.ID,“STU_NO”);
学生=excel.工作表中的c(“学生时间表”)
选择c;
List StudentList_c=Students_var.ToList();

为了进行工作单元测试,我简化了您的问题

我在本地创建值,而不是从excel文件中读取

在我创建的值中,我有3个项,但只有2个学生,因此我要验证linq查询始终返回2条记录

您可以在下面的代码中看到如何对项目进行分组

    [Test]
    public void LinqSelect_MultipleRowsWithTheSameId_RemoveDuplicatedRecords()
    {
        var excel = new [] {new Student() {ID = 1,Exam = 1}, new Student() { ID = 1, Exam = 2 }, new Student() { ID = 2, Exam = 1 } };
        IEnumerable<IGrouping<int,Student>> Students_var = from c in excel
                           group c by c.ID into newExcel
                       select newExcel;
        Assert.AreEqual(2, Students_var.ToList().Count);
    }

    public class Student
    {
        public int ID { get; set; }
        public int Exam { get; set; }
    }
[测试]
public void LinqSelect\u multiplerowswithsameid\u removedreplicatedrecords()
{
var excel=new[]{new Student(){ID=1,考试=1},new Student(){ID=1,考试=2},new Student(){ID=2,考试=1};
IEnumerable Students\u var=来自excel中的c
按c.ID将c分组到新Excel中
选择新建Excel;
Assert.AreEqual(2,Students_var.ToList().Count);
}
公立班学生
{
公共int ID{get;set;}
公共整数检查{get;set;}
}

为了进行工作单元测试,我简化了您的问题

我在本地创建值,而不是从excel文件中读取

在我创建的值中,我有3个项,但只有2个学生,因此我要验证linq查询始终返回2条记录

您可以在下面的代码中看到如何对项目进行分组

    [Test]
    public void LinqSelect_MultipleRowsWithTheSameId_RemoveDuplicatedRecords()
    {
        var excel = new [] {new Student() {ID = 1,Exam = 1}, new Student() { ID = 1, Exam = 2 }, new Student() { ID = 2, Exam = 1 } };
        IEnumerable<IGrouping<int,Student>> Students_var = from c in excel
                           group c by c.ID into newExcel
                       select newExcel;
        Assert.AreEqual(2, Students_var.ToList().Count);
    }

    public class Student
    {
        public int ID { get; set; }
        public int Exam { get; set; }
    }
[测试]
public void LinqSelect\u multiplerowswithsameid\u removedreplicatedrecords()
{
var excel=new[]{new Student(){ID=1,考试=1},new Student(){ID=1,考试=2},new Student(){ID=2,考试=1};
IEnumerable Students\u var=来自excel中的c
按c.ID将c分组到新Excel中
选择新建Excel;
Assert.AreEqual(2,Students_var.ToList().Count);
}
公立班学生
{
公共int ID{get;set;}
公共整数检查{get;set;}
}

您可以按学生ID分组,然后将每个考试添加到学生对象中

var grouped  = StudentList_c.GroupBy(x => x.ID).Select(x => new Student
        {
            ID = x.Key,
            Exam = x.SelectMany(z=> z.Exam).ToList()
        }).ToList();


public class Student
{
    public int ID { get; set; }
    public List<int> Exam { get; set; }
}
var group=StudentList\u c.GroupBy(x=>x.ID)。选择(x=>newstudent
{
ID=x.键,
Exam=x.SelectMany(z=>z.Exam).ToList()
}).ToList();
公立班学生
{
公共int ID{get;set;}
公共列表考试{get;set;}
}

您可以按学生ID分组,然后将每个考试添加到学生对象中

var grouped  = StudentList_c.GroupBy(x => x.ID).Select(x => new Student
        {
            ID = x.Key,
            Exam = x.SelectMany(z=> z.Exam).ToList()
        }).ToList();


public class Student
{
    public int ID { get; set; }
    public List<int> Exam { get; set; }
}
var group=StudentList\u c.GroupBy(x=>x.ID)。选择(x=>newstudent
{
ID=x.键,
Exam=x.SelectMany(z=>z.Exam).ToList()
}).ToList();
公立班学生
{
公共int ID{get;set;}
公共列表考试{get;set;}
}
var rows=excel.Worksheet(“Stu_Schedule”).ToList();
var groupedStudents=rows.GroupBy(y=>y.Id)
.选择(x=>new Student
{
ID=x.键,
Exam=x.SelectMany(z=>z.Exam).ToList()
}).ToList();
var rows=excel.Worksheet(“Stu_Schedule”).ToList();
var groupedStudents=rows.GroupBy(y=>y.Id)
.选择(x=>new Student
{
ID=x.键,
Exam=x.SelectMany(z=>z.Exam).ToList()
}).ToList();


Student类有一个考试列表,但我没有提到确切的声明excel中的行数是动态的而不是2,这2是什么意思?Assert.AreEqual(2,Students_var.ToList().Count);我已经更改了Var声明,因此可以更清楚地看到linq表达式通过分组返回您的对象我的单元测试只是演示linq查询,我正在创建硬编码的学生列表,并直接对其进行查询,而不使用任何excel文件…这给了我一个异常“LinqToExcel不支持Group()方法”学生班有一个考试列表,但我没有提到确切的声明excel中的行数是动态的而不是2,这2是什么意思?Assert.AreEqual(2,Students_var.ToList().Count);我已经更改了Var声明,因此可以更清楚地看到linq表达式通过分组返回您的对象我的单元测试只是演示linq查询,我正在创建硬编码的学生列表,并直接对其进行查询,而不使用任何excel文件…这给了我一个异常“LinqToExcel不支持Group()方法”它给了我一个异常“LinqToExcel不支持Group()方法”“GroupBy方法正在使用System.Linq;我已经编辑了用于StudentList_c变量的代码。这应该行。请你检查一下好吗?当然,我会看一下。如果我的回答有帮助,请投票。如果它回答了您关于分组数据的问题,请选择它作为答案,它会给我一个异常“LinqToExcel不支持Group()方法”“GroupBy方法正在使用System.Linq;我已经编辑了用于StudentList_c变量的代码。这应该行。请你检查一下好吗?当然,我会看一下。如果我的回答有帮助,请投票。如果它回答了您关于分组数据的问题,请选择它作为答案,告诉我错误})。ToList();>>>错误1运算符“.”无法应用于“lambda expression”类型的操作数抱歉,我现在没有linq2Excel。这是我记忆中的参考资料。对于select(x=>newstudent{ID=x.Key,Exam=x.Sel)仍然给出相同的错误