2

I have the following three tables, and need to bring in information from two dissimilar tables.

  • Table baTable has fields OrderNumber and Position.
  • Table accessTable has fields OrderNumber and ProcessSequence (among others)
  • Table historyTable has fields OrderNumber and Time (among others).

.

var progress = from ba in baTable                                   
           from ac in accessTable
           where ac.OrderNumber == ba.OrderNumber
           select new {
                Position = ba.Position.ToString(),
                Time = "", 
                Seq = ac.ProcessSequence.ToString()
           };
progress = progress.Concat(from ba in baTable
                       from hs in historyTable
                       where hs.OrderNumber == ba.OrderNumber
                       select new {
                           Position = ba.Position.ToString(),
                           Time = String.Format("{0:hh:mm:ss}", hs.Time),
                           Seq = ""
                       });
int searchRecs = progress.Count();

The query compiles successfully, but when the SQL executes during the call to Count(), I get an error

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

Clearly the two lists each have three items, one of which is a constant. Other help boards suggested that the Visual Studio 2010 C# compiler was optimizing out the constants, and I have experimented with alternatives to the constants.

The most surprising thing is that, if the Time= entry within the select new {...} is commented out in both of the sub-queries, no error occurs when the SQL executes.

1
  • Try changing Time = String.Format("{0:hh:mm:ss}", hs.Time) to Time = "foo" Commented Oct 27, 2011 at 17:51

1 Answer 1

1

I actually think the problem is that Sql won't recognize your String.Format(..) method.

Change your second query to:

progress = progress.Concat(from ba in baTable
                       from hs in historyTable
                       where hs.OrderNumber == ba.OrderNumber
                       select new {
                           Position = ba.Position.ToString(),
                           Time = hs.Time.ToString(),
                           Seq = ""
                       });

After that you could always loop trough the progress and format the Time to your needs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.