如何对两个实体进行内部连接并输出到另一个 class 类型

How do I do an inner join on two entities and output to another class type

我正在围绕 Access 数据库创建一个 entity framework 包装器,但我无法修改架构。

我有一个名为 Component 的实体和一个名为 TerminalInfo 的实体。关系是1:0..1。我想将结果映射到名为 Terminal.

的第三个 class

这是我目前所拥有的。

我收到以下语法错误。

Predefined type 'System.ValueTuple`2' is not defined or imported

public class TerminalUtility
{
    public ProjectContext context { get; private set; }

    public TerminalUtility(ProjectContext context)
    {
        this.context = context;
    }

    public IQueryable<Terminal> GetTerminals()
    {
            IQueryable<Terminal> terminals = context.TerminalInfos
            .Join(
                context.Components,
                C => C.Id,
                TI => TI.Id,
                (C, TI), 
                new Terminal(C,TI)
            );

        return terminals;
    }
}


[Table("Component")]
public class Component
{
    [Key]
    [Column("Counter")]
    public int Id { get; set; }

    [Column("Name")]
    public string Name { get; set; }

    public string HigherName { get; set; }

    public string LowerName { get; set; }

    public int SortOrder { get; set; }
    /*Various other not relavent properties */
}

[Table("CompPart")]
public class TerminalInfo
{
    [Key]
    [Column("Counter")]
    public int Id { get; set; }
    [Column("CompPartName")]
    public string LevelName { get; set; }
    [Column("CompPartSort")]
    public short LevelSorting { get; set; }
    /*Various other not relavent properties */
}

public class Terminal
{
    private Component _comp;
    private TerminalInfo _term;

    public Terminal(Component comp, TerminalInfo term)
    {
        _comp = comp;
        _term = term;

    }

    private int _Id;
    public int Id { get => _Id; set { _Id = value; _comp.Id = value; _term.Id = value; } }

    public string ProductName => _comp.Name; //** Name - Component

    public string TerminalBlock { get => _comp.HigherName; set => _comp.HigherName = value; } //** HigherName in Component

    public string TerminalNumber { get => _comp.LowerName; set => _comp.LowerName = value; } //* LowerName in Component

    public int TerminalSortOrder { get => _comp.SortOrder; set=> _comp.SortOrder = value; } //** SortOrder in Component

    public string LevelName { get=> _term.LevelName; set => _term.LevelName = value; } //** CompPartName in CompPart

    public short LevelSorting { get => _term.LevelSorting; set => _term.LevelSorting = value; } //** CompPartSort in CompPart

}

预计到达时间:这与

无关

问题是 Lambda 表达式中的拼写错误。

看来您只是有一个容易被忽视的拼写错误。根据 MSDN,Join method 语法采用三个 lambda 作为其最后三个参数,但您传递的是两个 lambda、一个元组和一个新对象。

您的 GetTerminals 方法应为:

public IQueryable<Terminal> GetTerminals()
{
        IQueryable<Terminal> terminals = context.TerminalInfos
        .Join(
            context.Components,
            C => C.Id,
            TI => TI.Id,
            (C, TI) => new Terminal(C,TI) //This is a lambda expression
        );

    return terminals;
}

将第三个 lambda 表达式保留在一行中可能有助于在您下次查看代码时弄清楚代码,即使 MSDN 实际上确实在 =>.

之后添加了一个换行符