1

I have two tables in my database Element and Entity, given an el_id(element id) i would like to extract the ent_id(entity id) from the Element table and ent_type_id(Entity type) from Entity table, i have an ent_id column common in both the tables but it is int in Element table and String in the Entity table, following is the code, help me resolve it.

  var ent_ids = _context.Elements.Where(e => el_ids.Contains(e.el_id)).Select(el => el.ent_id);
  var ent_type_ids = _context.Entities.Where(e => ent_ids.Contains(e.ent_id)).Select(el => el.ent_type_id);

Error:

Instance argument: cannot convert from 'System.Linq.IQueryable(string)' to 'System.Linq.ParallelQuery(int)'

This comes for the second line of the code.

Edit: added models from comment below.

Entities Model:

public partial class Entity 
{ 
    public int ent_id { get; set; } 
    public Nullable<int> document_id { get; set; } 
} 

Elements Model:

public partial class Element 
{ 
    public int el_id { get; set; } 
    public string ent_id { get; set; } 
}
Eonasdan
  • 7,563
  • 8
  • 55
  • 82
Prithvi514
  • 143
  • 3
  • 13

1 Answers1

0

Check out Brian Cauthon answer on this post. You should be able to do something like this:

//according to the models you posted...

//this is a collection of strings right?
var ent_ids = _context.Elements.Where(e => el_ids.Contains(e.el_id)).Select(el => el.ent_id);

//e.ent_id should be an int right?.
var ent_type_ids = _context.Entities.Where(e => ent_ids.Contains(SqlFunctions.StringConvert((double)e.ent_id))).Select(el => el.ent_type_id);

For whatever reason there is no overload of SqlFunctions.StringConvert() that takes int, so you have to cast to double.

Community
  • 1
  • 1
Ben Tidman
  • 2,129
  • 17
  • 30
  • ErrorCannot convert type 'string' to 'double' – Prithvi514 Jan 08 '13 at 18:20
  • Do i have my assumptions backwards? Can you show the models you are using? – Ben Tidman Jan 08 '13 at 18:25
  • @Prithvi Dammalapati i updated my answer with some comments. can you please verify that what I am suggesting is correct by stepping through your code. It seems like what I have should work based on the models you provided. – Ben Tidman Jan 08 '13 at 18:56
  • var el_ids = _context.Ent_El_Tags.Where(t => tag_list.Contains(t.tag_id)).Select(e => e.entel_id); var ent_ids = _context.Elements.Where(e => el_ids.Contains(e.el_id)).Select(el => el.ent_id); var ent_type_ids = new List(); foreach (var el in ent_ids) { if (el != null) { var id = int.Parse(el); ent_type_ids.Add(id); } } return _context.Entity_Type.Where(e => int_type_ids.Contains(e.type_id)); } – Prithvi514 Jan 08 '13 at 19:21
  • Sorry for the un-formatted code, but i am not sure how to do it, this is how i made it work – Prithvi514 Jan 08 '13 at 19:23