Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'

like image 626
Richard77 Avatar asked Nov 17 '14 20:11

Richard77


1 Answers

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>

like image 107
Habib Avatar answered Oct 20 '22 08:10

Habib