Al Muzahid Ratul
Al Muzahid Ratul

Reputation: 58

Combine two set of same entities

I have this class

public class Subject
{
    [Required]
    public int Id { get; set; }

    public string SubjectName { get; set; }

    public string Technology { get; set; }
}

I have two queries to select data from that class.
Query A

var subjectA = _dbContext.Subjects
                .Where(x => x.Technology == "Tech A")
                .Take(10)
                .ToList();

Query B

var subjectB = _dbContext.Subjects
                .Where(x => x.Technology == "Tech B")
                .Take(10)
                .ToList();

I need these two entities combined as one list of entity.
But, I couldn't find any example to do so.

EDIT: I need 10 from each of them.

Upvotes: 1

Views: 262

Answers (4)

Andy G
Andy G

Reputation: 19367

You need to include the OR operator || for 10 from either:

var subjectAB = _dbContext.Subjects
                .Where(x => x.Technology == "Tech A" || x.Technology == "Tech B")
                .Take(10)
                .ToList();

If you want 10 from each then use .Concat:

var subjectAB = _dbContext.Subjects
    .Where(x => x.Technology == "Tech A")
    .Take(10)
    .Concat(_dbContext.Subjects
        .Where(x.Technology == "Tech B")
        .Take(10))
    .ToList();

Concat joins the two sequences without attempting any work to remove duplicates that Union does.

Upvotes: 3

lgoncalves
lgoncalves

Reputation: 2090

Do you need the first 10 of each or 10 total, no matter what?

Could be:

_dbContext.Subjects
  .Where(x => x.Technology == "Tech A")
  .Take(10)
  .Union(_dbContext.Subjects
          .Where(x => x.Technology == "Tech B")
          .Take(10))
  .ToList();

Or:

_dbContext.Subjects
  .Where(x => x.Technology == "Tech A" || x.Technology == "Tech B")
  .Take(10) // Or 20
  .ToList();

Upvotes: 4

Patrick Huizinga
Patrick Huizinga

Reputation: 1340

When you have two lists of items and you want to combine them into a single list, you can use the Enumerable.Concat< T>(this IEnumerable< T>, IEnumerable< T>) extension method.

var bothSubjects = subjectA.Concat(subjectB);

Upvotes: 4

Koni
Koni

Reputation: 482

I'm not 100% sure if I understand your question But if you want to do a single query to get both technologies, you could simple do:

var subjects = _dbContext.Subjects
                .Where(x => x.Technology == "Tech A" || x.Technology == "Tech B")
                .Take(10)
                .ToList();

Though I would strongly reccomend you to select the techs by their IDs, not their string names.

For example: .where(x => x.TechnologyId == 1 (Number of your id)

var subjects = _dbContext.Subjects
                    .Where(x => x.TechnologyId == 1 || x.TechnologyId == 2
                    .Take(10)
                    .ToList();

You could also Query subject A, then query subject B and sum them both. That would depend on your requirement. But if you just need both, use the first query.

Upvotes: 0

Related Questions