【问题标题】:How to display all results in linq query如何在linq查询中显示所有结果
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    一旦键(主键、外键)匹配,它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      相关资源
      最近更新 更多