【发布时间】: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
我想比较这两个表(countryno 和branchcode),如果我没有第二个表值,请将它们添加到新表中,然后将其设为 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