Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4053682
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:28:41+00:00 2026-05-20T14:28:41+00:00

I’m struggling with an exception using linq-to-sql Concat() I’ve got 2 tables. The first

  • 0

I’m struggling with an exception using linq-to-sql Concat()

I’ve got 2 tables.
The first table, ParsedMessages, has the following fields

* ParsedMessageID (int)
* MessageTypeID (int)
* TextMessage (varchar(max)) 

The second table, ParsedMessageLinks, has the following fields

* ParsedMessageID (int)
* AnotherID (int)
* NumberOfOccurences (int)

This is what I need to achieve using a single linq query but I’m not sure if it’s possible or not.

  • Through a join, retrieves ParsedMessage records that links to a certain AnotherID. In example SQL and linq code, the AnotherID will have the value 0 just for the purpose of having an example.

  • For each ParsedMessage record, I also need the NumberOfOccurences (field of table #2)

  • Retrieve only the top(100) ParsedMessage records for each MessageTypeID. So for example, if there is 275 records in ParsedMessages that links to AnotherID==0 where the first 150 records have MessageTypeID == 0 and the remaining 125 records having MessageTypeID == 1, I want my query to end up returning 200 records, the top(100) with MessageTypeID == 0 and the top(100) with MessageTypeID == 1

After a lot of search, I’ve found that the plain SQL equivalent of I what I want to do is this. I knew that this exists first end, but I tried to find something else without Union all at first and fail to do so (my SQL knowledge is not that good) :

SELECT TOP(100) PM.*,
PML.NumberOfOccurences FROM
ParsedMessages PM INNER JOIN
ParsedMessageLinks PML ON
PM.ParsedMessageID =
PML.ParsedMessageID WHERE
PML.AnotherID = 0 AND PM.MessageTypeID
= 0 ORDER BY PM.ParsedMessageID DESC UNION ALL

SELECT TOP(100) PM.*,
PML.NumberOfOccurences FROM
ParsedMessages PM INNER JOIN
ParsedMessageLinks PML ON
PM.ParsedMessageID =
PML.ParsedMessageID WHERE
PML.AnotherID = 0 AND PM.MessageTypeID
= 1 ORDER BY PM.ParsedMessageID DESC UNION ALL

SELECT TOP(100) PM.*,
PML.NumberOfOccurences FROM
ParsedMessages PM INNER JOIN
ParsedMessageLinks PML ON
PM.ParsedMessageID =
PML.ParsedMessageID WHERE
PML.AnotherID = 0 AND PM.MessageTypeID
= 2 ORDER BY PM.ParsedMessageID DESC

So basically, the only way to retrieve the data I need is to do 3 sql queries in a single pass where only the PM.MessageTypeID is different for each query.

Now I wanted to achieve this using linq-to-sql. After googling, I’ve found that I could use the Linq Concat() method to reproduce a SQL Union All.
Here are some links pointing to what I thought would work :
http://blog.benhall.me.uk/2007/08/linq-to-sql-difference-between-concat.html

EF. How to union tables, sort rows, and get top entities?

I end up having this exception :
System.IndexOutOfRangeException : “Index was outside the bounds of the array.”

Here’s the faulty code :

IQueryable<MyObject> concatquery;
int[] allMessageTypeIDs = new int[] { 0, 1, 2 };
for (int mt = 0; mt < allMessageTypeIDs.Length; mt++)
{
    if (mt == 0)
    {
        concatquery = (from pm in db.ParsedMessages
                       join pml in db.ParsedMessageLinks on pm.ParsedMessageID equals pml.ParsedMessageID
                       where pml.AnotherID == 0 && pm.MessageTypeID == allMessageTypeIDs[mt]
                       orderby pm.ParsedMessageID descending
                       select new MyObject
                       {
                           NumberOfOccurences = pml.Occurrences,
                           ParsedMessage = pm
                       }).Take(100);
    }
    else
    {
        concatquery = concatquery.Concat(from pm in db.ParsedMessages
                                        join pml in db.ParsedMessageLinks on pm.ParsedMessageID equals pml.ParsedMessageID
                                        where pml.AnotherID == 0 && pm.MessageTypeID == allMessageTypeIDs[mt]
                                        orderby pm.ParsedMessageID descending
                                        select new MyObject
                                        {
                                            NumberOfOccurences = pml.Occurrences,
                                            ParsedMessage = pm
                                        }).Take(100);
    }
}
var results = concatquery.ToArray();

I’ve declared the int array allMessageTypeIDs, for simplicity. But remember that the values it holds may differ, so that’s why I’ve added the for loop. Maybe it’s “illegal” to use a Concat() in a loop that way, but I could not find any relevant information on this exception.

The class MyObject basically hold a int (NumberOfOccurences) and a ParsedMessage database object, nothing else.

Any suggestions on what could be wrong with my code that causes the exception?

Thanks
Francis

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:28:42+00:00Added an answer on May 20, 2026 at 2:28 pm

    Never use the variable you’re looping with in your Linq queries. It just doesn’t work. You want to assign a new temporary variable to use instead.

    IQueryable<MyObject> concatquery;
    int[] allMessageTypeIDs = new int[] { 0, 1, 2 };
    for (int mt = 0; mt < allMessageTypeIDs.Length; mt++)
    {
        var myItem = allMessageTypeIDs[mt]; // <-- HERE!
        if (mt == 0)
        {
            concatquery = (from pm in db.ParsedMessages
                           join pml in db.ParsedMessageLinks on pm.ParsedMessageID equals pml.ParsedMessageID
                           where pml.AnotherID == 0 && pm.MessageTypeID == myItem
                           orderby pm.ParsedMessageID descending
                           select new MyObject
                           {
                               NumberOfOccurences = pml.Occurrences,
                               ParsedMessage = pm
                           }).Take(100);
        }
        else
        {
            concatquery = concatquery.Concat(from pm in db.ParsedMessages
                                            join pml in db.ParsedMessageLinks on pm.ParsedMessageID equals pml.ParsedMessageID
                                            where pml.AnotherID == 0 && pm.MessageTypeID == myItem
                                            orderby pm.ParsedMessageID descending
                                            select new MyObject
                                            {
                                                NumberOfOccurences = pml.Occurrences,
                                                ParsedMessage = pm
                                            }).Take(100);
        }
    }
    var results = concatquery.ToArray();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.