DataTable does not contain definition for AsEnumerable - c#

Using linq to query a datatable returns the following error: CS0117: 'DataSet1.map DataTable' does not contain a definition for 'AsEnumerable'
Project includes reference for System.Data.Datasetextensions.
Here's the code.
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Reflection;
using System.Data;
using System.Linq;
using System.Data.Linq;
using System.Data.Common;
using System.Data.DataSetExtensions;
using System.Linq.Expressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
protected void Page_Load(object sender, EventArgs e)
{
var query1 = from mfg_nm in DataSet1.mapDataTable.AsEnumerable()
select mfg_nm;
}
running it w/out AsEnumerable() results in
var query1 = from mfg_nm in DataSet1.mapDataTable
select mfg_nm;
CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type.

The method you want is in the System.Data namespace, so that using directive is fine, but you also need a reference to the System.Data.DataSetExtensions assembly. Are you sure you've got that reference as an assembly reference?
It's not clear why you've got a using directive for a System.Data.DataSetExtensions namespace - does that not raise an error?
What is the exact error with the AsEnumerable() call? (I'm surprised about the error you're getting with the second form... that's not the error I'd have expected.)

Add System.Data.DataSetExtensions from "nuget" or "add reference"
Add this code:
using System.Data.DataSetExtensions;

In all cases where this happens, the reference to System.Data.DataSetExtensions.dll was missing. If in doubt, try creating a simple console project targeting .NET 4 with a reference to System.Data.DataSetExtensions.dll, to verify that adding the reference actually works.
Also note that you only need to use the System.Data namespace.
BTW mapDataTable is a DataTable, right?

I got this error message:
'System.Data.DataTable' does not contain a definition for 'AsEnumerable' and no extension method 'AsEnumerable' accepting a first argument of type 'System.Data.DataTable' could be found (are you missing a using directive or an assembly reference?)
Added
using System.Data;
Added "System.Data.DataSetExtensions" to the References section. It resolved the problem.

Google search "system.data.datatable does not contain a definition for asenumerable" brought me here, and my trouble was missing:
using System.Data;
Due to my implement the error message was a bit misleading. Hence, my answer to this question. Code was like...
public List<mytype> MyMethod(params) {
return new mynamespace.myclasslib.myclass().GetDataTable(params).AsEnumerable()
.etc
}
Once I tried to explicitly declare the DataTable, it became obvious that I was missing the using statement.

Try this code :
DataSet1.mapDataTable.Select().AsEnumerable()

Related

using directive is unnecessary visual studio - random class

I want to use Random class but I got this error message:
using directive is unnecessary visual studio
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Random; //I got this error at this line
namespace Mikrotik
{
....
Quick-fix:
You only need using System; - inside your code you can use Random directly.
Background:
Please read the documentation for C#'s using directive: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive
I think you're imagining that using works like how import works in Java where you have to name each type you want to bring into scope or use .* to bring all types into scope. Instead, C#'s using only imports namespaces, making it roughly equivalent to Java's import being used exclusively with the .* syntax.
You're getting the error because there is no namespace System.Random, instead it's a type-name; so you just need using System; - in your code you can use Random as-is.
For pendantry: C#'s using directive has 3 different modes:
using namespace; - brings into scope all types in the specified namespace
using Alias = namespace.TypeName; - brings into scope a single type, but with an alias. The alias can match the original name.
using static namespace.TypeName; - brings only the static members of the specified type into scope.
Not to be confused with C#s using() {} statement, which is completely unrelated to importing types and namespaces.

SmartTag for adding a "using" statement for extension method

Lets say I have the following code that needs to be moved to another file/class:
namespace MyNamespace
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Class1
{
public void SomeMethod()
{
IEnumerable<int> l = new List<int>();
if (l.Count() == 0)
{
//...
}
}
}
}
If I move SomeMethod to another class (in another file) that does not have (say) a using System.Linq declaration, the l.Count() statement would not compile giving the very obvious message:
'System.Collections.Generic.IEnumerable<int>' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'System.Collections.Generic.IEnumerable<int>' could be found (are you missing a using directive or an assembly reference?)
If the new class/file does not have a using System.Collections.Generic declaration and you put your cursor on the IEnumerable<int> you will get a SmartTag that would suggest possible using-declarations. If you put your cursor on the Count() extension call you get no SmartTag to suggest the using System.Linq declaration.
Is there maybe an IDE extension out there that would help add a using statement for extension methods as explained above? This would help allot with refactoring code.
I have found a solution that would be free:
Download and install DevExpress CodeRush Express and then download and install the CR_ExtensionMethodsHelper community plugin.
You could also try the following commercial products (not free):
DevExpress CodeRush (with CR_ExtensionMethodsHelper community plugin; although there is a request pending to have this plugin supported natively)
ReSharper (I'm not using this one my self, but others have suggested it)

A using namespace directive can only be applied to namespaces

using System.Text.RegularExpressions;
using System.DateTime;
DateTime returnedDate = DateTime.Now();
it give me error :
A using namespace directive can only be applied to namespaces;
'System.DateTime' is a type not a namespace (line 1, pos 1)
where is my mistake?
where is my mistake?
It is here: using System.DateTime;
DateTime is a class inside System namespace, not a namespace. In C# you can apply using directive only to namespaces. Adding using XYZ to your program lets you omit the namespace prefix XYZ from classes inside that namespace - for example, to reference class XYZ.ABC you can write ABC. The using directory does not go down to class level, though (this is in contrast to Java's import directories, where .* at the end of the name is optional).
Fix this by replacing using System.DateTime; with using System;
EDIT : (in response to a comment by Karl-Johan Sjögren) There is another using construct in C# that lets you create aliases of types. This construct takes class names, but requires you to specify a new name for them, like this:
using DT = System.DateTime;
Now you can use DT in place of System.DateTime.
using System;
DateTime returnedDate = DateTime.Now();
You should use namespace like this way:
using system;
OR this way with out using namespace:
System.DateTime returnedDate = System.DateTime.Now();
using System;
DateTime returnedDate = DateTime.Now();
DateTime is a type which means its a class.
C# keyword "using" can only be used with namespaces. so in order to use DateTime class in your code , you don't need to write like this.
using System.DateTime;
Rather than writing above line,Simply Include System Namespace like this.
using System;
And use DateTime class in code.
In C# 6 you can do
using static System.DateTime;
var now = Now;

'MovieDataContext' could not be found

I am following the tutorial
http://www.asp.net/mvc/tutorials/older-versions/models-(data)/creating-model-classes-with-linq-to-sql-cs.
I'm getting the following error:
The type or namespace name 'MovieDataContext' could not be found (are you missing a using directive or an assembly reference?)
As the error message says:
(are you missing a using directive or an assembly reference?
So add the correct using clause to bring the namespace into which this class is defined into scope:
using MvcApplication1.Models;
Check this Listing 1 – Controllers\HomeController.cs
using System.Linq;
using System.Web.Mvc;
using MvcApplication1.Models; // you have forgotten to include this
Listing 1 uses a LINQ to SQL DataContext class (the MovieDataContext) to represent the MoviesDB database. The MoveDataContext class was generated by the Visual Studio Object Relational Designer.
If you have done the work of creating the LINQ to SQL Classes then
just include the using statements as in the listing 1

"using" statement locations within namespace declarations [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Should Usings be inside or outside the namespace
I'm supporting some code that, unusually, has all its using statements contained within the namespace declaration. It makes me a bit uncomfortable but I have no idea if this should be anything to be concerned about or not. I can't think of an immediate problem other than it being counter to usual convention.
Is there anything wrong with:
namespace myProject.controls
{
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
Instead of the more widely-used:
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace myProject.controls
{
There's nothing to be concerned with here; in fact, the first example (with the using statements inside the namespace declaration) is recommended by default by StyleCop.
The only difference has to do with scoping. In the first case, the using declarations are scoped within the namespace declaration, and are not available outside it, in other namespace declarations in the same file, for example. In the second case, the declarations are in scope for the whole file.
Location of the using statement
affects the scope of the references
within the file.
I cannot think of any situation that would 'warrant' limiting it withing a smaller scope. Though, I would prefer sticking to conventions - or commenting exhaustively - , just to make sure 'next person' does not have to guess my intentions.
Probably this link will answers your question in detail
C# - Location of using statements

Categories

Resources