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 8028255
In Process

The Archive Base Latest Questions

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

I have a linq query that pulls down some records. It could be 1

  • 0

I have a linq query that pulls down some records. It could be 1 record, 2 records or 30 records. I have two textboxes I want the records to go into if there is only 1 record or only 2 records then it if its over that it puts it in a dropdownlist. This is what I have so far.

var getContacts = (from r in gServiceContext.CreateQuery("account")
join c in gServiceContext.CreateQuery("contact") on ((EntityReference) r["accountid"]).Id
equals c["accountid"]
where r["accountid"].Equals(ddlCustomer.SelectedValue)
select new
{
    FirstName = !c.Contains("firstname") ? string.Empty : c["firstname"],
    LastName = !c.Contains("lastname") ? string.Empty : c["lastname"],
});

foreach (var contact in getContacts)
{
    if (getContacts.ToList().Count() == 1)
    {
        txtContact1Name.Text = contact.FirstName + " " + contact.LastName;
    }
    else if (getContacts.ToList().Count() == 2)
    {
        txtContact2Name.Text = contact.FirstName + " " + contact.LastName;
    }
    else if (getContacts.ToList().Count() > 2)
    {
        ddlMultipleContacts.DataSource = getContacts;
        ddlMultipleContacts.DataTextField = "LastName";
        ddlMultipleContacts.DataValueField = "LastName";
        ddlMultipleContacts.DataBind();
    }
}

But it puts the same record in textbox 1 and textbox two if there are two records. Am I doing something wrong?

Thanks!

  • 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-05T00:05:07+00:00Added an answer on June 5, 2026 at 12:05 am

    But it puts the same record in textbox 1 and textbox two if there are two records. Am I doing something wrong?

    Yes. Look at your code:

    if (getContacts.ToList().Count() == 2)
    

    You’re calling that on each iteration – you’re not using the count of how many contacts you’ve already used. I suspect you want:

    // Let's only materialize the results *once* instead of once per iteration...
    var contacts = getContacts().ToList();
    switch (contacts.Count)
    {
        case 0: // What do you want to do here?
            break;
        case 1:
            txtContact1Name.Text = FormatName(contacts[0]);
            break;
        case 2:
            txtContact1Name.Text = FormatName(contacts[0]);
            txtContact2Name.Text = FormatName(contacts[1]);
            break;
        default:
            ddlMultipleContacts.DataSource = contacts;
            ddlMultipleContacts.DataTextField = "LastName";
            ddlMultipleContacts.DataValueField = "LastName";
            ddlMultipleContacts.DataBind();
            break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LINQ query that pulls down some information based on certain criteria
I have a LINQ to ENTITY query that pulls from a table, but I
I have a LINQ query that returns some object like this... var query =
I have a linq query that is causing some timeout issues. Basically, I have
I'm trying to construct a linq query that pulls all nodes that have a
I have written a LINQ to XML query that does what I want, but
I have a LINQ query that I want to order by the most recently
I have a LINQ query that pulls ID values with integers: var data =
I have a LINQ query that starts with var LinqQuery. But I want to
I have a List of X items. I want to have LINQ query that

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.