cannot convert from 'AnonymousType#1' to 'int'

9.2k views Asked by At

All I need to get all the team members ids so that I can query the contact table.

var teamMembersIds = (from q in _repository.GetQuery<TeamMember>
                               (t => teamIds.Contains(t.TeamId))
                                select new { q.ResourceContactId }
                     )
                    .ToList();

The problem is that I need to merge it with another anonymous list of ids.

resContactIds.AddRange(teamMembersIds);

I'm getting the following error:

I tried also this:

var resContactIds = new List<int>();
foreach (var _id in teamMembersIds)
{
   if(resContactIds.Contains(_id))
   {
      resContactIds.Add(_id);
   }
}

I'm getting the following error: cannot convert from 'AnonymousType#1' to 'int'

1

There are 1 answers

1
Habib On BEST ANSWER

With select new { q.ResourceContactId } you are selecting an anonymous type, if you want an List<int> then remove new and curly braces like::

var teamMembersIds = (from q in _repository.GetQuery<TeamMember>
                               (t => teamIds.Contains(t.TeamId))
                                select q.ResourceContactId //here
                     )
                    .ToList();

The problem is that I need to merge it with another anonymous list of ids

Your other list resContactIds is also List<int>, it is not a list of anonymous objects.

One more thing to add, you can omit the call ToList in your first query since AddRange can accept IEnumerable<T>