I dont know a whole lot about C# but I have this project I am working on where I would like to do something like this:
SortedDictionary<int, List<ChessMove>> possibleMovesByRank = new SortedDictionary<int, List<ChessMove>>();
...
var best = possibleMovesByRank.Keys.Last();
From what I have been able to find, this should use linq to return the key that has the highest value, however VS is giving me an error:
SortedDictionary.KeyCollection does not contain a definition for 'Last' and no extension method for 'Last'
Am I missing something or is my project not set up correctly or something?
Most likely, you are missing the System.Linq namespace in your C# file. This is an Extension Method and Enumerable.Last will not exist unless you include the relevant namespace.
Related
I am trying to check whether a SearchResultEntry contains a certain attribute. For this, I am trying the following code:
if(searchResultEntry.Attributes.AttributeNames.Contains(propertyName))
but this fails with the error
ICollection does not contain a definition for Contains, and the best extension method overload "Queryable.Contains<string>(IQueryable<string>, string)" requires a receiver of type "IQueryable<string>".
I also tried to make it an IQueryable using AttributeNames.AsQueryable(), but on that IQueryable, Contains is not available as well.
What is wrong here? When IntelliSense tells me something is available, but then, when I got to that very type, it is still unavailable, am I missing some references or using directives?
To clear up the obvious first, I am:
using System.Linq;
using System.Collections.Generic;
Have a project reference to System.Core
My DbSet and DbContext classes are defined properly
The strange thing is, intellisense sees it, but I have a compiler warning and it won't compile.
I have tried cleaning the project first, restarting Visual Studio, etc and it still is complaining about:
Error CS1929 'DbSet' does not contain a definition for 'ToList' and the best extension method overload 'Enumerable.ToList(IEnumerable)' requires a receiver of type 'IEnumerable'
My "receiver" in this case is the compiler statically-determined var.
This is a new laptop and a fresh Visual Studio install ... is there something obvious I am missing here?
I wanted these internal but tried switching everything pubilc just to be sure, but I am getting the same error.
Most people report this when they are missing the using clause for Linq but I have that, and intellisense sees it which is where I am getting confused.
Line in question:
var excludedUrls = db.ExcludedUrls.ToList<string>();
I assume that in your code db is a Dbcontext with a property ExcludedUrls of type DbSet<TEntity>
DbSet<TEntity> implements IEnumerable<TEntity> If you have included LINQ, you can use the extension method Enumerable.ToList<TEntity>().
Because of your error I assume that TEntity is not a string. Your code will compile if you leave out the part:
var excludedUrls = db.ExcludedUrls.ToList();
However, this might not give a list with the elements you want. Somehow you'll have to convert each excludedUrl in your source sequence to the string representation that you want. If ToString() does not do this, you'll have to create a function yourself:
class MyDbContext : DbContext
{
public DbSet<MyTSource> ExcludedUrls {get; set;}
}
void string Transform(MyTSource excludedUrl)
{
...
}
List<string> result = dbContext.ExcludedUrls
.Select(excludedUrl => Transform(excludedUrl)
.ToList();
In words: from the sequence of excludedUrls, take each element, and transform it to a string using Transform. Convert the resulting sequence of strings to a list, which will be a list of strings
By the way, did you notice I used ToList without mentioning <string>? The compiler is able to detect the type of the elements in your input sequence, and can create a List of the same type of elements.
Consider avoiding mentioning the TSource when using the Linq functions. This has several advantages:
less typing
Easier to change the source sequence, or intermediate linq statements
Easier to detect the resulting type (and thus easier to detect the error you mentioned)
It allows the use of anonymous types
The answer, it turns out, is not calling the generic form of ToList<> here, but instead calling straight ToList(), which returns a generic List<ExcludedUrl>. A thank you to Ivan Stoev in the comments section for the tip.
I was relying too much on Intellisense hints here and not enough on memory.
As you can see, due to the way extension methods work, it presented me with a ToList in its generic form, which is why my code was written the way it was.
I decided that constantly writing a long winded code to select data from a table was a bad method to use when I am constantly having to reference between tables, therefore I decided to write a class that handles the queries for me and returns the rows.
public Database(object query, object name)
{
var tbl = (from c in db.name where query select c).ToArray();
return tbl;
}
I am sort of new to Entity and am having a hard time trying to understand the type name should be.
At first I tried object which gave me this error:
'Model.Container' does not contain a definition for 'name' and no extension method 'name' accepting a first argument of type 'Model.rkdb_07022016Entities2' could be found (are you missing a using directive or an assembly reference?)
So I knew that the table name I am selecting from cannot be passed through as type object.
I took an educated guess and tried to make name type Queryable but still no luck.
Could anyone possibly point me in the right direction and add some references where I can maybe learn how to tackle these situations in the future?
Thank-you.
(putting as an answer for future viewers)
You probably want an Expression<Func<EntityType, bool>>
If you need to make it generic, you could build something with T.
As I said in the comment, it's worth taking a look at Joseph Albahari's PredicateBuilder for sample code (or indeed use it as it is powerful)
I'm using the PagedList.Mvc NuGet Package for paging, In a View everything seems to works perfectly nice but in my controller this error occurs
'System.Collections.Generic.List<CreateSocial.Models.UserProfiles>' does not contain a definition for 'ToPagedList' and no extension method 'ToPagedList' accepting a first argument of type 'System.Collections.Generic.List<CreateSocial.Models.UserProfiles>' could be found (are you missing a using directive or an assembly reference?)
even though I defined the namespace like:
using PagedList;
using PagedList.Mvc;
my code look like this:
[HttpGet]
public ActionResult Results(SearchViewModel svm, int CurrentPage)
{
const int ResultsPerPage = 50;
List<UserProfiles> results = db.UserProfiles.Where(w => w.FirstName.Contains(svm.SearchStr) || w.LastName.Contains(svm.SearchStr)).ToList();
return View(results.ToPagedList(CurrentPage, ResultsPerPage));
}
I tried deleting and adding PagedList again via Package Manager Console. But I can't seem to fix it. What could be causing this and how to fix it? Hope you guys can give any suggestions. Thanks in advance!
First of all don't use ToList()!
If you have like 10.000 records in database it will first load them to memory and then execute method ToPagedList
If you look in pagedlist extensions definition then you will see that you can also use iqueryable
// Summary:
// Creates a subset of this collection of objects that can be individually accessed
// by index and containing metadata about the collection of objects the subset
// was created from.
//
// Parameters:
// superset:
// The collection of objects to be divided into subsets. If the collection implements
// System.Linq.IQueryable<T>, it will be treated as such.
//
// pageNumber:
// The one-based index of the subset of objects to be contained by this instance.
//
// pageSize:
// The maximum size of any individual subset.
//
// Type parameters:
// T:
// The type of object the collection should contain.
//
// Returns:
// A subset of this collection of objects that can be individually accessed
// by index and containing metadata about the collection of objects the subset
// was created from.
public static IPagedList<T> ToPagedList<T>(this IQueryable<T> superset, int pageNumber, int pageSize);
About yours problem, check if you have referenced pagedlist and pagedlist.mvc. Also try rebuild project and check if any other errors are shown
You're probably missing a "using xxxxxx" namespace.
using PagedList;
These are usually "extension methods"......and while they extend something that you probably already have a "using" statement for...the extension method itself is probably in another namespace (thus requires an additional "using" statement).
APPEND
You mention "I tried deleting and adding PagedList again via Package Manager Console"
When you do this, there is a drop down box for the target project. Make sure you have the correct csproj listed in the drop down box.
To make super sure you have the reference, open up your proj file (csproj) in notepad or notepad++ and look for the reference.
Following this tutorial http://msdn.microsoft.com/en-us/vstudio/hh543922.aspx , I'm trying to use the ReplaceNode method that should be in the SyntaxNode class.
The thing is, I have this error: "Roslyn.Compiler.CSharp.SyntaxNode does not contain a definition for 'ReplaceNode'
Any ideas?
ReplaceNode is actually an extension method (so that it can return the type passed in). Make sure you have using Roslyn.Compilers; and using Roslyn.Compilers.CSharp;
Looks like an issue with that version of the CTP. Others are having the same issue, such as http://youtrack.jetbrains.com/issue/DOTP-4774 or http://youtrack.jetbrains.com/issue/DOTP-4836 .