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

  • SEARCH
  • Home
  • 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 8054613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:11:18+00:00 2026-06-05T08:11:18+00:00

I am joining two datatables and selecting using LINQ. I am getting an error

  • 0

I am joining two datatables and selecting using LINQ. I am getting an error in the ‘orderby’ line. “The name ‘contact’ does not exist in the current context”. If I order by columns from the ‘customer’ variable it works, but not using columns from the ‘contact’ variable.

Then I removed the orderby line and tried to order it using lambda expression like:

orders.OrderBy(o => (string) o["ContactName"]; 

and I got an error “Cannot apply indexing with [] to an expression of type ‘AnonymousType#1’. I don’t want to create a new class just for sorting.

Any ideas how to do this kind of sorting. I will be doing a sort using multiple columns from both tables.

CODE:

  var orders = (from customer in Customers.AsEnumerable()
                          join contact in Contacts.AsEnumerable()
                              on (int) customer["CustomerID"] equals (int) contact["CustomerID"] into outer
                                from contact in outer.DefaultIfEmpty()
                                orderby contact["ContactName"]   
                          select new
                                     {
                                         ContactID = (int) contact["ContactID"]),
                                         Name =  (string) contact["ContactName"]),
                                         City =  (string) contact["City"])
                                  });
  • 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-06-05T08:11:20+00:00Added an answer on June 5, 2026 at 8:11 am

    There are some syntax problems(too many paranthesis, f.e. contact["ContactID"])), this should work:

    var orders =  from customer in Customers.AsEnumerable()
                  join contact in Contacts.AsEnumerable()
                  on customer.Field<int>("CustomerID") equals contact.Field<int>("CustomerID") into outer
                      from contact in outer.DefaultIfEmpty()
                      orderby contact["ContactName"]   
                      select new
                      {
                            ContactID = contact.Field<int>("ContactID"),
                            Name =  contact.Field<String>("ContactName"),
                            City =  contact.Field<String>("City")
                      };
    

    Sidenote: i would use the strong type Field extension method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a case of Linq, where upon joining two tables, I'm getting 10
ASP.NET 3.5 C# I am joining two tables using Linq. Table names are MapAssets
I'm joining two tables - Contact and RetailTrainingUserLevelMap in a Select statement. (The common
There's a lot of info about joining two tables, however I'm not interested in
I am joining two images using the code below but it throws an OutOfMemory
Does the Win32 API have a function for joining two paths? I can't find
I have a query which is equi-joining two tables, TableA and TableB using a
I am joining two tables 'sales/order_item_collection' and 'sales/orders' by 'order_id', so that afterward i
I have a relatively simple query joining two tables. The Where criteria can be
How would I go about joining two lambda expressions like theese: Expression<Func<string, bool>> expr1

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.