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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:11:31+00:00 2026-05-25T10:11:31+00:00

I am working on a new project and we are using Entity Framework and

  • 0

I am working on a new project and we are using Entity Framework and the dev lead would like to use lambda queries whenever possible. One thing we are having a hard time figuring out is how to select two columns specifically. Also how to select distinct. We have a table that has multiple entries for a vendor but we want to just get a list of vendors and load to a dictionary object. It fails because as written it is trying to add a key value that has already been added. Take the following query.

Dictionary<int, string> dict = new Dictionary<int, string>();
        dict = GetWamVendorInfo().AsEnumerable()
                    .Where(x => x.vendor_name != null && x.vendor_id != null)
                    //.Select(x => x.vendor_id).Distinct()
                    .Take(2)
                    .ToDictionary(o => int.Parse(o.vendor_id.ToString()), o => o.vendor_name);

What I would like to do is select just vendor_id and vendor_name so we can get just the distinct records.

Any help would be greatly appreciated.

Thanks,

Rhonda

  • 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-25T10:11:32+00:00Added an answer on May 25, 2026 at 10:11 am

    Use an anonymous type:

    // earlier bit of query
    .Select(x => new { VendorId = x.vendor_id, VendorName = x.vendor_name } )
    .Distinct()
    .ToDictionary(o => o.VendorId, o => o.VendorName);
    

    I’ve removed the call to Take(2) as it wasn’t clear why you’d want it – and also removed the parsing of VendorId, which I would have expected to already be an integer type.

    Note that you should almost certainly remove the AsEnumerable call from your query – currently you’ll be fetching all the vendors and filtering with LINQ to Objects. There’s also no point creating an empty dictionary and then ignoring it entirely. I suspect your complete query should be:

    var vendors = GetWamVendorInfo()
                          .Select(x => new { VendorId = x.vendor_id,
                                             VendorName = x.vendor_name } )
                          .Distinct()
                          .ToDictionary(o => o.VendorId,
                                        o => o.VendorName);
    

    As an aside, you should ask your dev lead why he wants to use lambda expressions (presumably as opposed to query expressions) everywhere. Different situations end up with more readable code using different syntax options – it’s worth being flexible on this front.

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

Sidebar

Related Questions

I am working on a new project that makes use of Entity Framework. I
I'm coming from a MS SQL Server background. Working on a new project using
I'm working on a new project and I'm using the repository pattern, I have
I am working on a project using rails 2.1.1. With the new release of
I am working on a web service project using gsoap. I am new to
I'm working on a new project where I want to use Phil Haack Areas
I'm working on a new project with a full ANT build. I use eclipse
I am new to Entity Framework, and ORM's for that mather. In the project
I'm using Entity Framework 4 in my project. The Framework has created its own
I'm working on a new project and we're using a pretty nice stack. NHibernate,

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.