将linq中的int转换为join子句中的实体

时间:2014-11-07 08:19:59

标签: c# linq linq-to-entities

我试图加入两个表,比较一个int类型的列和一个varchar类型的列,但是没有我尝试的东西我得到一些转换错误...

using (Ginkgo5Entities dataContext = new Ginkgo5Entities())
{
    var x = (
        from node in dataContext.new_users_teams
            join udata in dataContext.new_users_data
                on new {
                    x = SqlFunctions.StringConvert((double)node.ID),
                    y = "team" }
                equals new {
                    x = udata.value,
                    y = udata.key }
        from parent in dataContext.new_users_teams
            where node.lft >= parent.lft && node.lft <= parent.rgt && parent.ID == 6
        select new {
            team = node.team_name
            // udata = udata }
    ).ToList();
}

抛出

&#34; {指定的方法&#39; System.String StringConvert(System.Nullable`1 [System.Double])&#39;在类型&#39; System.Data.Objects.SqlClient.SqlFunctions&#39;无法转换为LINQ to Entities商店表达式。}&#34;

问题:

  1. 我甚至可以尝试一下吗?如果是的话......任何人都可以帮忙
  2. 我这是一个不好的做法?
  3. 非常感谢!

1 个答案:

答案 0 :(得分:2)

尝试将此查询更改为:

var x = (from node in dataContext.new_users_teams
                         join udata in dataContext.new_users_data   on node.ID equals udata.value.ToString() and udata.key equals "team" }
                         from parent in dataContext.new_users_teams

                         where node.lft >= parent.lft && node.lft <= parent.rgt && parent.ID == 6