1

我从 web 表单项目中选择了这个语句,我想转换它,以便我可以在我的 mvc 项目中使用,以便在我的剃须刀页面的下拉列表中显示

<asp:SqlDataSource ID="Sections" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>" 
                SelectCommand="SELECT e.DisplayName,  e.ID , e.GUID
                FROM Table1 e
                INNER JOIN RootTables re
                ON e.ID = re.Table1ID
                AND re.ChairID = 1">
            </asp:SqlDataSource>

我想它开始像:var sCommand = (from e in Table1....);

4

1 回答 1

4

这很简单。查询应该是这样的:

var results = 
    from e in db.Table1
    join re in db.RootTables re
    on e.ID equals re.Table1ID
    where re.ChairID == 1
    select new { e.DisplayName, e.ID, e.GUID };

或流利的语法:

var results = 
    db.Table1.Join(db.RootTables, 
        e => new { e.ID, ChairID = 1 }, 
        re => new { ID = re.Table1ID, re.ChairID },
        (e, re) => new { e.DisplayName, e.ID, e.GUID }); 

如果您已正确设置导航属性,它会变得更加容易:

var results = 
    from e in db.Table1
    where e.RootTables.Any(re => re.ChairID == 1)
    select new { e.DisplayName, e.ID, e.GUID };

或流利的语法:

var results = db.Table1.Where(e => e.RootTables.Any(re => re.ChairID == 1))
                       .Select(e => new { e.DisplayName, e.ID, e.GUID });
于 2013-07-29T17:16:21.137 回答