C# linq 表达式未正确提取数据

标签 c# linq entity-framework-core

我正在尝试查询两个表,并且需要从两个表中提取相关记录。我正在使用 enityframeworkcore 3 一个是系统版本表,另一个是历史表。我的结果集仅包含历史表中的数据,而不包含系统版本表中的数据。有人可以告诉我我的陈述有什么问题吗?我确保 systemversion 表中的 personid 与历史记录表匹配。

enter image description here

查询

  var personNotes = (from pn in _context.PersonNotes
                              join pnh in _context.PersonNotesHistory on pn.PersonId equals pnh.PersonId
                              select pn);
            return personNotes;

最佳答案

您需要指定要在结果中返回的列名称:

var personNotes = (from pn in _context.PersonNotes
                              join pnh in _context.PersonNotesHistory on pn.PersonId equals pnh.PersonId
                              select new {
                                           data1 = pn.column1,
                                           data2 = pn.column2,
                                           data3 = pn.column3,
                                           data4 = pnh.column1,
                                           data5 = pnh.column2,
                                           data6 = pnh.column3,


                                          }).ToList();
            return personNotes;

只需将 column1、column2、column3 更改为您要从数据库检索的列名称即可。

关于C# linq 表达式未正确提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59567981/

相关文章:

c# - DataGridView更改单元格背景并恢复默认样式

c# - 如何计算字符串中的行数?

c# - lambda 表达式中的 GroupBy

c# - Entity Framework 核心: many-to-many relationship with the same object

c# - 是否可以仅在单击按钮时使用 XAML 启动另一个 View ?

c# - 带有链接按钮的 ASP.net Visual Studio Repeater

c# - 如何在函数中将委托(delegate)作为 <T> 参数传递

.net - 非常复杂的 LINQ(to SQL)查询示例

c# - EF Core 如何选择具有多对多关系的实体

c# - 使用 SQLite 数据库 : unable to create object of type 进行 EF Core 迁移