farhang67
farhang67

Reputation: 789

the best overloaded method match for List<T>.Add(T) has some Invalid arguments

I defined a List like this:

List<Agahii.Ads> ads = new List<Agahii.Ads>();

Then I want to fill it with the result of a LINQ query:

for (int i = 0; i < adid.Count(); i ++ )
{
      var dd = adid[i];
      var cc = (from a in context.Ads where a.AdID == dd select a).ToList();

      ads.Add(cc);     // error appears here
};

error appears in the ads.Add(cc); line.

error: the best overloaded method match for System.Collections.Generic.List.Add(Agahii.Ads) has some Invalid arguments

Upvotes: 5

Views: 13768

Answers (2)

Grant Winney
Grant Winney

Reputation: 66479

The Add method is used to add a single object.

Try AddRange instead, which allows you to add a list:

ads.AddRange(cc);

You can shorten the whole method with some LINQ. Something like this should work:

var ads = context.Ads.Select(a => adid.Contains(a.AdID)).ToList()

Upvotes: 6

John Saunders
John Saunders

Reputation: 161801

Use

ads.AddRange(cc);

because cc is a list, not an element.

Upvotes: 2

Related Questions