简体   繁体   中英

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

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()

Use

ads.AddRange(cc);

because cc is a list, not an element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM