I'm trying to execute a SQL query for a WCF service because the database I'm using doesn't have a stored procedure to return data I need. You can see the code below. I'm using VS2010 and connecting to Microsoft SQL Server. The Exception I get is: The multi-part identifier could not be bound. Thanks for the help.
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;
}
you are missing a bunch of spaces, so that's part of the problem. Add spaces (at the end of the lines)!
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");
Your original query looked something like SELECT BLAH FROM CODES t1inner join (MOREBLAH)...
. Specifically, look at the t1inner
. That's what is gumming up the query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With