4

我正在尝试为 WCF 服务执行 SQL 查询,因为我使用的数据库没有存储过程来返回我需要的数据。你可以看到下面的代码。我正在使用 VS2010 并连接到 Microsoft SQL Server。我得到的异常是:无法绑定多部分标识符。谢谢您的帮助。

        public List<ObjectCodeGroup> GetObjectCodeGroup()
    {
        TCDataDataContext dc16 = new TCDataDataContext();
        List<ObjectCodeGroup> results = new List<ObjectCodeGroup>();

        var objectCodeResults = dc16.ExecuteQuery<ObjectCodeGroup>(@"select t1.codeid," +
            "t1.code, t1.catalogtype, t1.codegroup, t1.codetext, t1.codegrouptext, t1.codedesc, t1.state_id from CODES t1" +

            "inner join ( select MIN(codeid) codeid, codegroup from CODES group by codegroup)" +
                "t2 on t1.codeid = t2.codeid and t1.codegroup = t2.codegroup WHERE catalogtype = 11");

        foreach (ObjectCodeGroup o in objectCodeResults)
        {
            results.Add(new ObjectCodeGroup()
           {
               codegrouptext = o.codegrouptext
           });
        }

        return results;
    }
4

1 回答 1

7

你错过了一堆空格,所以这是问题的一部分。添加空格(在行尾)!

var objectCodeResults = dc16.ExecuteQuery<ObjectCodeGroup>(@"select t1.codeid, " +
"t1.code, t1.catalogtype, t1.codegroup, t1.codetext, t1.codegrouptext, t1.codedesc, t1.state_id from CODES t1 " +
"inner join ( select MIN(codeid) codeid, codegroup from CODES group by codegroup) " +
"t2 on t1.codeid = t2.codeid and t1.codegroup = t2.codegroup WHERE catalogtype = 11");

您的原始查询看起来像SELECT BLAH FROM CODES t1inner join (MOREBLAH).... 具体来说,看t1inner. 这就是混淆查询的原因。

于 2013-07-03T14:43:57.593 回答