System.Collections.Generic.List Does Not Contain a Definition for 'Select'

System.Collections.Generic.List<string>' does not contain a definition for 'add'

Try using Add instead of add,

selectionStrings.Add(string.Format("{0} AS {1}", selector, col));

also initialize selectionStrings , currently your code will throw an exception

List<string> selectionStrings = new List<string>();

Error with SortedList: "Does not contain a definition for Select."

Note: I'm posting this as a completely new answer because the recent edit to your question has made it clear that the real problem is quite different from what it first seemed.

  1. Select the subset from data that you want in your resulting SortedList<…>:

    var subset = data.Where(x => x.Key >= startDate && x.Key <= endDate); 
  2. Build a new SortedList from the filtered items:

    var sortedListSubset = new SortedList<DateTime, BarData>();

    foreach (var subsetItem in subset)
    {
    sortedListSubset.Add(subsetItem.Key, subsetItem.Value);
    }

    I don't see an easier solution. The problem is that Enumerable.Where will return an IEnumerable<KeyValuePair<DateTime, BarData>>, and there's no extension method to convert that back to a SortedList<…>. Thus the need to iterate through the IEnumerable<…> item by item and add them to a fresh SortedList<…>.

When to use Where and when to use Select:

  • Use Where whenever you want to find a subset, ie. when you want to "filter" a collection. (If you know SQL well, Where corresponds to WHERE, hence the name.)

  • Use Select when you want to change/transform, or "project", each element of a collection into something else. In functional programming, this operation is often called "map". (Again, if you know SQL well, then Select — unsurprisingly — corresponds to SELECT.)


Finally, remember that instead of data.Where(x => x.Key >= …), you could have written:

 var subset = from x in data 
where x.Key >= …
select x

This is some syntactic sugar that C# allows, but essentially means the same thing (however in this form, the select cannot be omitted, even if it doesn't really do any projection). Note again the similarity to SQL!

C# error CS1061: Type `System.Collections.Generic.List<int>' does not contain a definition for `Length'

There is no Length property on List<T>. Use Count instead and for other classes that implement ICollection.

Length is typically only for arrays.

IList does not contain a definition <linq function>

If your collection does not implement the generic interfaces you can use the Cast or OfType(Cast + Filter) extension methods.

IList list = GetList();
string first = list.Cast<string>().FirstOrDefault();

You can use these methods with anything that implements IEnumerable. Of course it works only if the list really contains strings. If it could contain anything you could use:

string first = list.Cast<Object>().Select(obj => obj?.ToString() ?? "").FirstOrDefault();

If you just want objects that are strings you can use OfType as mentioned above:

string first = list.OfType<string>().FirstOrDefault();

System.Collections.Generic.IEnumerable' does not contain any definition for 'ToList'

Are you missing a using directive for System.Linq?

https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tolist

IEnumerable<List<DataRow>> does not contain a definition for ToList()

How about:

List<string> ItemNames = DT.AsEnumerable()
.Select(ro => ro["Name"].ToString()) //pull the name out of the datarow, projecting an enumerable of the names. Remember that ro is a DataRow, and ro["Name"] is an object hence the ToString (could also have cast)
.Distinct() //reduce to just distinct names
.ToList(); //turn it to a list

Here we pull all the names, then use Distinct() to distinct them, rather than forming a grouping - I think this will be less resource intensive than tracking the key and the associated rows

As to what went wrong with your original, you were misunderstanding how a grouping works. This here will create something a bit like a Dictionary<string, List<DataRow>>():

from r in DT.AsEnumerable()
group r by r.Field<string>("Name") into rr

Every unique name in the table becomes the dictionary key. And then every row that has that same name goes into the dictionary value (a list of datarows). When you said:

select rr; //you said r, but i renamed it to rr to make it more clear what is going on, this is a new object unrelated to your original r

rr is the value of the dictionary item; it is a List of all DataRows that have the same name. Could be one item in that list, could be thousands. It is NOT the name of the person that went into the Key of the Grouping. It is the value of the Grouping; the value of the grouping is a List<DataRow> that all have the same name and are hence stored under one key. If it were a Dictionary<string, List<DataRow>>:

List<DataRow> allPeopleCalledJohn = myDictionary["John"];

You could have selected the key, or pulled the name out of the first row in the list

i.e. You could have:

List<string> ItemNames = new List<string>();
var query1 = from r in new DataTable().AsEnumerable()
group r by r.Field<string>("Name") into rr
select rr.Key; //its the key of the grouping, i.e. the name

ItemNames = query1.ToList<string>();

Or even:

List<string> ItemNames = new List<string>();
var query1 = from r in new DataTable().AsEnumerable()
group r by r.Field<string>("Name") into rr
select rr.First().Field<string>("Name"); //its the name of the first DataRow indexed by the grouping

ItemNames = query1.ToList<string>();

But why I don't suggest these is it's a pointless waste of resources to ask LINQ to go to the effort of making a grouping set, and tracking every datarow back to the name it holds, when all you want is the name itself. Instead if you pull just the names out, distinct them, and list them, then you can ditch the rest of the row data at the first step rather than carrying it to the end then throwing it away

System.Array does not contain a definition for 'Any' - C#

You need using System.Linq; for that to work.

Any is an extension method defined in LINQ.

Also, pay attention to the type of ProductNameArray. If it is defined as Array (and not string[], for example), the compiler has no way of inferring that, when enumerated, it'll yield strings.

In that case, you'd have to write:

if (ProductNameArray.Cast<string>().Any(usersearch.Contains))

Edit: OK, looking at the code it seems that the problem is the one described above.

You'll have to change the signature of the FindProduct method from

static void FindProduct(Array ProductNameArray)

to

static void FindProduct(string[] ProductNameArray)

or use the .Cast<string> method.

I'd personally prefer changing the method's signature, since the ProductNameArray passed to it seems to really be a string[].

System.Collections.Generic.List<Plate>' does not contain a definition for 'Time' and no extension method 'Time'

usersQuery is an IEnumerable<IEnumerable<Plate>> you can either rectify this in your platesQuery like this:

var platesQuery = from plates in usersQuery
from plate in plates
group plate by plate.Time into grouping
select grouping;

Or flatten out the initial results so you have an IEnumerable<Plate> instead:

var usersQuery = from user in Model.Users
from plate in user.Plates
select plate;


Related Topics



Leave a reply



Submit