3

I need to get a list of records and filter them based on a condition: if serviceId = 1 then I need to combine that result with the result for serviceId = 5.

Models:

public class Partner
{
    [Key]
    public int Id { get; set; }
    public DbGeography Location { get; set; }
    public virtual ICollection<PartnerServiceBrand> PartnerServiceBrands { get; set; }
}

public class PartnerServiceBrand
{
    [Key]
    public int Id { get; set; }

    public virtual Partner Partner { get; set; }
    public virtual Service Service { get; set; }
}

public class Service
{
    [Key]
    public int Id { get; set; }
    public virtual ICollection<PartnerServiceBrand> PartnerServiceBrands { get; set; }
}

My code for just one filter:

var serviceId = 1;

var partners = dbContext.Partners.Where(p => p.PartnerServiceBrands.Select(psb => psb.Service.Id).Contains(serviceId));

I tried to do:

if (serviceId == 1)
{
    var partners2 = dbContext.Partners.Where(p => p.PartnerServiceBrands.Select(psb => psb.Service.Id).Contains(5));

    partners = partners.Union(partners2);  // Error
}

I also tried to use Contains with a List<int>, but I was not able to get it up and running.

EDIT

The error I get is:

Exception:Thrown: "The geography data type cannot be selected as DISTINCT because it is not comparable." (System.Data.SqlClient.SqlException)

4
  • It's not clear what you want to do. What do you need in the first list? Do you want to get all the partners that have a Service with id = 1? Commented May 13, 2015 at 0:44
  • 1
    What error message you got? Commented May 13, 2015 at 5:16
  • @ataravati hi, I want to get all the partners that have the serviceId=5 joined with all the partners that have the serviceId=1 Commented May 13, 2015 at 21:24
  • @techkid hi, sorry I have edited the question with the error and the missing property that is causing the error (public DbGeography Location { get; set; }) Commented May 13, 2015 at 21:30

4 Answers 4

1

My take on this.

Given the list of serviceIds that you want to filter on:

var serviceIds = new List<int>{1, 5};

var partners = dbContext.Services
    .Where(ser => serviceIds.Contains(ser.Id).SelectMany(ser => ser.PartnerServiceBrands)
    .Select(psb => psb.Partner).Distinct();
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I have to add a ")" after the Contains(ser.Id) to avoid red editor underline, but in execution I get the error I have included in the question
1

You're getting an error because Union() generates UNION ALL with SELECT DISTINCT. As an aside Concat() produces just a UNION ALL.

In any case, I think you should be able to get what you're looking for with something like below:

var serviceIds = new List<int>{ 1, 5 };
var partners = dbContext.Partners
    .Where(p => p.PartnerServiceBrands.Any(psb => serviceIds.Contains(psb.Service.Id))

Comments

0

Maybe this will works:

var partners = dbContext.Services
    .Where(ser => ser.Id == 1 || s.Id == 5).SelectMany(ser => ser.PartnerServiceBrands)
    .Select(psb => psb.Partner).Distinct();

Comments

0

Assuming you have a DbSet<Service> as Services, I would try :

var serviceId = ...;

var partners = dbContext.Services
      .Where(svc => svc.Id == serviceId || (serviceId == 1 && svc.Id == 5))
      .SelectMany(svc => svc.PartnerServiceBrands)
      .Select(psb => psb.Partner).Distinct();

1 Comment

I get repeated partners :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.