0

I have heard that using contain method degrades Entity Framework query performance.

My class file:

public partial class VendorInCategory
{
        public int Id { get; set; }
        public int CategoryId { get; set; }
        public int VendorId { get; set; }

        public virtual CategoryMaster CategoryMaster { get; set; }
        public virtual UserDetails UserDetails { get; set; }
}

This is my query:

List<int> categoryIds = new List<int>();

for (int i = 0; i < Items.Count(); i++)
{
    categoryIds.Add(Convert.ToInt32(Items.ElementAt(i)));
}

var data = context.VendorInCategory
                  .Where(x => ((categoryIds).Contains(x.CategoryMaster.Id)) 
                   {
                        -------------------
                   }

Can anybody tell how do I compare this list without using contain keyword???

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

You could try something like this:

var l = from c in context.VendorInCategory
        join id in categoryIds on c.CategoryMaster.Id equals id into cid
        from id in cid.DefaultIfEmpty()
        select new {contextEntry = c, idEntry = id};

var data = (from d in l where d.idEntry != null select d.ContextEntry).ToList();

The first part joins the two, setting idEntry to null if there is no entry found. The second part retrives all of the VendorInCategory items from the context where CategoryMaster.Id exists in categoryIds.

Jacob Lambert
  • 7,449
  • 8
  • 27
  • 47
  • i dont want to write query in this form.i want to write projection query only like i have written the query –  Nov 01 '14 at 06:39
  • 1
    Your question is marked with `linq` which is what this is. I'm not sure exactly how this will map to methods. But I use this same format for my ETL and it will process 100,000+ entries in 0.1 sec on average. – Jacob Lambert Nov 01 '14 at 06:43
  • plz check this link:http://stackoverflow.com/questions/374267/contains-workaround-using-linq-to-entities.can u plz try with my query???? –  Nov 01 '14 at 06:55