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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:16:45+00:00 2026-05-23T03:16:45+00:00

I’m building an auction site and the user can bid on the same item

  • 0

I’m building an auction site and the user can bid on the same item more than once (obviously). In the user’s dashboard, a user can view his bids. When the user bids on the same item more than once, I want only one entry with the highest bid value to show up. My current code shows an entry for each bid. I tried a few things but I couldn’t figure it out. Here’s what I’ve got:

public class Bid
{
    public int Id { get; set; }
    public double Amount { get; set; }
    public DateTime Date { get; set; }
    public virtual Item Item { get; set; }
    public virtual User User { get; set; }
}

    protected override List<ItemForUserBids> ResolveCore(User source)
    {
        var items = new List<ItemForUserBids>();
        var userBids = source.Bids;
        foreach (var bid in userBids)
        {
            var item = bid.Item;
            var c = new ItemForUserBids
                        {
                            BidValue = bid.Amount,
                            BidId = bid.Id,
                            Description = item.Description,
                            Id = item.Id,
                            ItemThumb = item.MainImageLink(),
                            Status = _itemsService.GetBiddingStatus(item, source),
                            TimeLeft = item.EndDate.TimeLeft(),
                            Title = item.Title
                        };
            items.Add(c);
        }
        return items;
    }

I tried to get Distinct bids based on the Item.Id but that did not work. Now I’m thinking maybe I could use the Date property of the Bid entity somehow to get to the result I want but my head stopped thinking.

Any suggestions?

UPDATE:

I got it to work using a dictionary and using OrderBy() and Max() like many suggested. But I think the latter could be further improved.

Implementation using a dictionary (works):

    var userBids = new Dictionary<string, Bid>();

    foreach (var bid in allUserBids)
    {
        var key = bid.Item.Id.ToString();
        if(userBids.ContainsKey(key))
        {
            if (userBids[key].Amount < bid.Amount)
                userBids[key] = bid;
        }
        userBids[key] = bid;
    }

Attempt using the other method (works):

    var highestBids =
        source.Bids.Where(x => x.Date > DateTime.Now.AddYears(-1))
                    .GroupBy(x => x.Item.Id,
                                            (itemId, bids) =>
                                            new
                                                {
                                                    ItemId = itemId,
                                                    MaxBid = bids.Max(x => x.Amount)
                                                }).ToList();
    var userBids = new List<Bid>();
    foreach (var bid in source.Bids)
    {
        for(var i = 0; i < highestBids.Count; i++)
        {
            var curr = highestBids[i];
            if (bid.Item.Id.Equals(curr.ItemId) && bid.Amount.Equals(curr.MaxBid)) {
                userBids.Add(bid);
                highestBids.Remove(curr);
            }
        }
    }

How do I get rid of those loops? And maybe have it all in one chained statement?

  • 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-23T03:16:46+00:00Added an answer on May 23, 2026 at 3:16 am

    The comments posted so far should be a good indication that you should look into re-architecting this a little, but the immediate code solution involves using System.Linq to chain together a GroupBy, Max, and a Select.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a view passing on information from a database: def serve_article(request, id): served_article

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.