【发布时间】:2016-10-29 16:53:42
【问题描述】:
我有一个 linq 查询,它从两个表中获取结果。但该查询并未显示所有结果。当我在 SQL Server 中测试它时,我有这个:
| mcod | pcod | c_ogrn | fam_v | im_v | ot_v | idGk
| 41004 | 10023 | 1027800518063 | Johny | Johnson | Andrew | 3
| 41004 | 10023 | 1027800518063 | Johny | Johnson | Andrew | 11
您看到最后一列中的数据不同。但是当我在 linq 查询中进行测试时,我得到了这个结果:
| mcod | pcod | c_ogrn | fam_v | im_v | ot_v | idGk
| 41004 | 10023 | 1027800518063 | Johny | Johnson | Andrew | 3
| 41004 | 10023 | 1027800518063 | Johny | Johnson | Andrew | 3
为什么查询相同但结果不同?
我的 C# 代码:
var fidn = from post in repository.users
join meta in repository.usersLG on post.pcod equals meta.pcod
where post.fam_v.Trim() == "Johny" && post.actual == 1
//where post.fa post.fam_v.Trim() == fambox.Text and post.actual=1
select new Final
{
mcod = post.mcod,
pcod = post.pcod,
c_ogrn = post.c_ogrn,
fam_v = post.fam_v,
im_v = post.im_v,
ot_v = post.ot_v,
idGK = meta.idGK
};
我的 T-SQL 查询:
SELECT
users.[mcod], users.pcod, [c_ogrn], [fam_v], [im_v], [ot_v], idGk
FROM
[table].[dbo].[users]
JOIN
[table].[dbo].[usersLG] ON users.pcod = usersLG.pcod
WHERE
users.fam_v = 'Johny'
AND users.actual = 1
Asp.net 网页、实体框架、C# linq、SQL Server
更新我的类和主键:
用户:
[Key]
public string pcod { get; set; }
public string mcod { get; set; }
public string c_ogrn { get; set; }
public string fam_v { get; set; }
public string im_v { get; set; }
public string ot_v { get; set; }
public int actual { get; set; }
用户LG
[Key]
public string pcod { get; set; }
public int idGK { get; set; }
public string mcod { get; set; }
public byte actual { get; set; }
最终
[Key]
public int idGK { get; set; }
public string pcod { get; set; }
public string mcod { get; set; }
public string c_ogrn { get; set; }
public string fam_v { get; set; }
public string im_v { get; set; }
public string ot_v { get; set; }
public int actual { get; set; }
【问题讨论】:
-
users和usersLG的主键是什么? -
@GertArnold 用户 - pcod。 usersLg-pcod。我需要创建另一个主键吗?
-
@asdnafn No- 仅在 usersLg 中
-
通常,重复数据表明数据库和 EF 中的唯一标识不匹配。换句话说,EF 不使用数据库中定义的完整主键。
-
问题是,数据库中的主键是否和EF中定义的一样(
[Key]注解)?
标签: c# asp.net sql-server linq