如何在linq查询中显示所有结果

本文关键字:显示 结果 查询 linq | 更新日期: 2023-09-27 18:15:39

我有一个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
                          };

My 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 Web page, Entity Framework, c# linq, SQL Server

UPDATE my classes and primary key:

用户:

[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; }

usersLG

[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; }

如何在linq查询中显示所有结果

我解决了我的问题,谢谢Gert Arnold。
我的问题是相同值的行。因为我没有定义id columns -结果是相同的。
我只是从表中添加id列到我的类,并为它们添加主键。
现在一切都好了!

我有同样的问题,数据库中的主键应该与实体框架定义中的定义相同。

一旦键(主键,外键)匹配,它应该工作