如何在两个实体上进行内连接并输出到另一个类类型

时间:2017-08-09 01:02:17

标签: c# entity-framework linq jet-ef-provider

我正在创建一个围绕Access DB的实体框架包装器,我无法修改架构。

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

这是我到目前为止所做的。

我收到以下语法错误。

  

未定义或导入预定义类型'System.ValueTuple`2'

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

}

ETA:这与Predefined type 'System.ValueTuple´2´ is not defined or imported

无关

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

1 个答案:

答案 0 :(得分:2)

看起来你只是容易被忽视的拼写错误。根据MSDN,Join method语法将三个lambda作为最后三个参数,但是你传递了两个lambdas,一个元组和一个新对象。

您的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确实在=>之后添加换行符。