-1
public class Auctions : List<Auction>
{ 
    // Functions here
}

public class Auction
{
    public string item_name { get; set; }
    public long starting_bid { get; set; }
}
Auctions allAuctions;   // With data
string distinctName;    // With data

List<Auction> itemSet = allAuctions.Where(auction => auction.item_name.Contains(distinctName)).ToList();

I'm trying to search through derived class

Auctions : List<Auction>

with a LINQ query. In the end, no matter what I try, I get either iterable object or base class (List<Auction>).

How do I change my code to get result as derived class?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
RickStead
  • 3
  • 2

1 Answers1

1

Just add a constructor on the Auctions type that takes a List as an argument and adds it by AddRange() method. or even you can override Explicit and/or Implicit operators on the Auctions type to be able to cast a List to the Auctions type or assign instances of List to the Auctions type.

public class Auctions : List<Auction>
{ 
    public Auctions(){}
    public Auctions(List<Auction> autions){
         if(auctions == null)
              throw new ArgumentNullException(nameof(auctions));
         this.AddRange(auctions);
    }
}

public static class AuctionsExtensions{
    public static Auctions ToAuctions(this IEnumerable<Auction> auctions){
        if(auctions == null) throw new ArgumentNullException(nameof(auctions));
        return new Auctions(auctions.ToList());
    }
}

// and then :
List<Auction> itemSet = allAuctions.Where(auction => auction.item_name.Contains(distinctName)).ToAuctions();

Masud Safari
  • 108
  • 8