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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:30:08+00:00 2026-05-11T17:30:08+00:00

I have an array of ListViewItems ( ListViewItem[] ), where I store a SalesOrderMaster

  • 0

I have an array of ListViewItems ( ListViewItem[] ), where I store a SalesOrderMaster object in each ListViewItem.Tag for later reference.

I have some code that right now, goes through each ListViewItem safely casts the .Tag property into a SalesOrderMaster object, then adds that object to a collection of SalesOrders, only after checking to make sure the order doesn’t already exist in that collection.

The process to compare sales orders is expensive, and I would like to convert this to a LINQ expression for clarity and performance. ( I also have the Parallel Extensions to .NET Framework 3.5 installed so I can use that to further improve LINQ performance)

So without further ado: This is what I have, and then what I want. ( what I want won’t compile, so I know I am doing something wrong, but I hope it illustrates the point )

What I have: ( Slow )

foreach (ListViewItem item in e.Argument as ListViewItem[])
            {
                SalesOrderMaster order = item.Tag as SalesOrderMaster;
                if ( order == null )
                {
                    return;
                }
                if (!All_SalesOrders.Contains(order))
                {
                    All_SalesOrders.Add(order);
                }
            }

What I want: ( Theory )

    List<SalesOrderMaster> orders = 
(from item in (e.Argument as ListViewItem[]).AsParallel() 
select new { ((SalesOrderMaster)item.Tag) }).Distinct();

EDIT: I know the cast is cheap, I said the “Compare”, which in this case translates to the .Contains(order) operation

EDIT: Everyone’s answer was awesome! I wish I could mark more than one answer, but in the end I have to pick one.

EDIT : This is what I ended up with:

List<SalesOrderMaster> orders = 
(from item in (e.Argument as ListViewItem[]) select (SalesOrderMaster) item.Tag).GroupBy(item => item.Number).Select(x => x.First()).ToList();
  • 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-11T17:30:09+00:00Added an answer on May 11, 2026 at 5:30 pm

    I see nobody has addressed your need to convert an anonymous type to a named type explicitly, so here goes… By using “select new { }” you are creating an anonymous type, but you don’t need to. You can write your query like this:

    List<SalesOrderMaster> orders = 
        (from item in (e.Argument as ListViewItem[]).AsParallel() 
        select (SalesOrderMaster)item.Tag)
        .Distinct()
        .ToList();
    

    Notice that the query selects (SalesOrderMaster)item.Tag without new { }, so it doesn’t create an anonymous type. Also note I added ToList() since you want a List<SalesOrderMaster>.

    This solves your anonymous type problem. However, I agree with Mark and Guffa that using a parallel query here isn’t you best option. To use HashSet<SalesOrderMaster> as Guffa suggested, you can do this:

    IEnumerable<SalesOrderMaster> query = 
        from item in (ListViewItem[])e.Argument
        select (SalesOrderMaster)item.Tag;
    
    HashSet<SalesOrderMaster> orders = new HashSet<SalesOrderMaster>(query);
    

    (I avoided using var so the returned types are clear in the examples.)

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

Sidebar

Ask A Question

Stats

  • Questions 109k
  • Answers 109k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Summarising my initial post and comments - there are several… May 11, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer (On my Eee 1000) xev reveals that the two-finger scroll… May 11, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer There's always the WS-Security standard: WS-Security (Wikipedia) .NET has its… May 11, 2026 at 9:21 pm

Related Questions

I have an array of numbers that potentially have up to 8 decimal places
I have an array of a few million numbers. double* const data = new
I have an array of items that are time sensitive. After an amount of
I have an array of hashes, and I want the unique values out of
I have an array of 1000 strings to load into a combo box. What

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.