【问题标题】:Entity compare except and insert problem?实体比较异常和插入问题?
【发布时间】:2019-04-11 08:22:34
【问题描述】:

数据库名称:S

表:学生

ID   NAME   COUNTRYNO   AGE   BRANCHCODE
----------------------------------------
1    Alex    001        25       05
2    Mary    002        26       09

数据库名称:P

PERSON

 NAME   COUNTRYNO   AGE   BRANCHCODE  
------------------------------------------
 John     127        45      04
 Elize    125        54      06

我想要新表:

数据库名称:S

NEWPERSON

 NAME   COUNTRYNO   AGE   BRANCHCODE  SITUATION
----------------------------------------------------
 John     127        45      04          0
 Elize    125        54      06          0

我想比较这两个表(countrynobranchcode),如果我没有第二个表值,请将它们添加到新表中,然后将其设为 0。

但是这段代码没有运行。如何在Entity Framework中解决?

var student=DbContext.Entities.Student.Select(a=> new { CountryNo =a.CountryNo, BranchCode=a.BranchCode
});     ------>  //studentcount:0

var person=DbContext.Entities.Student.Select(a=> new { CountryNo =a.CountryNo, BranchCode=a.BranchCode
});     ----> //personcount:0

var common=person.Except(student);   -----> //common:0

List<NEWPERSON> np= new List<NEWPERSON>();   ---> np:0
foreach(var item in common)   //it doesnt enter loop
{
    var ıtem=person.Single(persons=>persons.PERSON==item.PERSON && persons.CountryNo==item.CountryNo);
if(tempItem !=null)
{
    NEWPERSON newperson=new NEWPERSON
{  
     CountryNo=item.CountryNo,
     BranchCode=item.BranchCode,
     Age=item.Age,
     Name=item.Name,
     Situation=0
}
np.Add(newperson);
}

}

【问题讨论】:

  • 您可以检查您的公共变量是否有任何匹配项,如果没有匹配项,则使用相反的相交来获取差异,分配给您的 personInf 变量并将其情况值设置为 0..stackoverflow.com/a/5620298/3254405
  • 嗨。我使用 2.way 从学生中选择国家和分支代码,除了从人员中选择国家和分支代码。然后我想输出插入新人表。但我不能。我如何处理 Entity 或 SQL。你能帮忙写代码吗?
  • 在我看来,如果您在这里唯一的问题是编写代码,那么您应该能够做到。这里似乎有一些表和数据重复,请考虑使用 1 个带有额外列的表外键或状态..
  • But this code doesn't run. 什么代码?您没有发布任何内容。
  • @Flater Hi。我写了这段代码。但没有运行。

标签: c# entity-framework


【解决方案1】:

如果我正确理解您的要求,您需要从 Table Student 中提取不存在于 Table Student 中的值并将它们添加到 Table NewPerson。

现在首先要强调的是 Intersect 的使用。来自 MSDN documentation “相交的结果将产生一组匹配值的交集”。因此,通过将两个表相交,您的结果通常是一个空集。使用 Intersect 的另一个重要因素是相交对象必须是同一类型。例如 int 、 Person 或 Student 和“Anonymous”(这非常重要)。

代码的另一个问题是您创建了一个 NewPerson 的空列表并将其添加到 Student 表中。

我想出的解决方案如下:

获取学生和人员的完整对象列表

var students = DbContext.Entities.Student().ToList();
var people = DbContext.Entities.Person().ToList();

而不是 Intersect 使用 except 这会给你一组不匹配的元素:

    var peopleNotRegisteredAsStudents = people.Select(person => new { person.CountryNo, person.BranchCode }).Except(
        students.Select(student => new { student.CountryNo, student.BranchCode })
        );

将 peopleNotRegisteredAsStudents 中的结果映射为 NewPerson 列表:

    List<NewPerson> personInf = new List<NewPerson>();
    foreach (var item in peopleNotRegisteredAsStudents)
    {
        var tempItem = people.SingleOrDefault(person => person.BranchCode == item.BranchCode && person.CountryNo == item.CountryNo);
        if (tempItem != null)
        {
            NewPerson newPerson = new NewPerson
            {
                ID = tempItem.ID,
                Name = tempItem.Name,
                CountryNo = tempItem.CountryNo,
                Age = tempItem.Age,
                BranchCode = tempItem.BranchCode,
                Situation = 0
            };

            personInf.Add(newPerson);
        }
    }

然后添加到NewPerson表中

DbContext.Entities.NewPerson.AddRange(personInf);
DbContext.Entities.SaveChanges();

如果您需要将 NewPerson 结果添加到 Student 表,您可以简单地将 NewPerson 值映射为 Student 对象并将它们添加到 Student。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2013-12-02
    • 2015-04-27
    • 1970-01-01
    相关资源
    最近更新 更多