.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links


Posted By:      Posted Date: May 22, 2011    Points: 0   Category :ASP.Net


I'm really struggling with Linq in my controller, i was helped with my last problem but now i have the following problem with the following error:


My code is:


           var CList = (from d in List1
                        select d);
           foreach (var d in CList) ;




I've tried adding ToList(), .AsEnumerable() etc but still cannot get it to return list of data which is countries.

If i put Response.Write(CList.Count()) i get correct number of records 64

If i put a breakpoint on var CList, i can see the data in List1, but CList has a value of null

Can anyone see my problem



View Complete Post

More Related Resource Links

system.linq.enumerable.where Issue



I have a table "OSTAN" with two columns "ID" (Key index) and "NAME". I am trying to  construct a linq query to select the Name for a specific ID. I have not luck trying to construct the where clause. Here what I have so far:


using (XeledsEntities db = new XeledsEntities())
            string SelectedName = db.Ostans.Name.Where( Ostan=> Ostan.Equals(Request.QueryString["Ostan"])).FirstOrDefault(); 


Can anybody help me construct this where clause to set the Selected Name?




Can anyone see where i'm going wrong here as i get the following in my view:


My code is:


public ActionResult Index()
            List<GetCurrencyCountriesDataFieldsFromDB> List1 = new List<GetCurrencyCountriesDataFieldsFromDB>();
            List<GetCurrencyRatesFromDatabase> List2 = new List<GetCurrencyRatesFromDatabase>();

            _IGCFDB.GetCurrencyFromDataBase(List1, List2);

            var rate = from c in List2 where c.DboCurrency.Contains("GBP") select c;

            this.ViewData["Test"] = rate;

            return View();


and in view i have




The type 'System.Data.Linq.DataContext' is defined in an assembly that is not referenced. You must



This is frustrating, and I don't know how to solve it.

I have a strange problem. I am adding a LinqDatasource object, and set the context:

        protected void LinqDataSource_ContextCreating(object sender, LinqDataSourceContextEventArgs e)
            e.ObjectInstance = new KaruselaDataContext(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

Then I get this error:
The type 'System.Data.Linq.DataContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Linq, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.


On web.config I already have this:
<add assembly="System.Data.Linq, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

NOTE: The datacontext is in a different project (DLL proect) where I added a refernce to System.Data.Linq.

is there another way adding a refernce to a web project? or only though teh web.config?


System.Xml.Linq.XElement and XPathDocument


I have an XPathDocument that I want to save to a table using LINQ, but I get this error

cannot implicitly convert type 'System.Xml.XPath.XPathDocument' to 'System.Xml.Linq.XElement'

I create the XPathDocument from some (valid) xml contained in a resource file

XPathDocument xDoc = new XPathDocument(new StringReader(Resource1.TestXML));

when I try to assign it with LINQ, it flags up an error

NewDataItem.XML = xDoc  ----  error here

NewDataitem is a LINQ to SQL object created from the underlying table, 'NewDataItem.XML' is a column type of 'XML'

how do i (easily) convert xDoc so I can pass it into the LINQ object ?

Advanced Basics: The LINQ Enumerable Class, Part 2


This month Ken digs deeper into the System.Linq.Enumerable class and shows readers how to perform some magic data operations using System.Linq.Enumerable.

Ken Getz

MSDN Magazine September 2008

Advanced Basics: The LINQ Enumerable Class, Part 1.


Presented here is the LINQ Enumerable class, which allows you to manipulate data in any class that implements IEnumerable(Of T).

Ken Getz

MSDN Magazine July 2008

The type or namespace name 'Linq' does not exist in the namespace 'System'

Hi, I uploaded my website to the server, and I have there a working with wcf - it should be fw 3.5 I saw that fw 3.5 is installed on the server and also we have there fw 3.0 sp1. I run my website : www.qenglishcenter.com and I'm getting an error : The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?) shouldn't System.linq be already existed ? Thanks for any reply.  

Question on WCF service with Linq to SQL on 64-bit system

I'm trying this question one more time since I realized the work in in a 64-bit environment. I have a WCF service which runs in production in a 32-bit environment using VS2008 and .NET 2.0. We made a big change to our environment (lots at the same time) and I moved the services to VS2010, .NET 4.0, and 64-bit processes.  I wrote a new service and deployed it under IIS.  If I browse to the service I get the example screen and clicking on the link gives me the metadata.  So all of that is working. In my services I create a Linq to SQL context, perform a query, then return the result set as an Array.  I wrapped the context creation with a using.  My tests returned an exception "Attempt to access an object that has already been disposed".  I traced it down and found that the code executes, forces the query, returns the results, exits the using statement (disposing the context properly) then exits the method.  The next thing I get is the exception, inside framework code, and it is linq code attempting to fulfill the query once again. I have searched until I'm blue in the face trying to find prior incidents and fixes but all I found was one post, two years ago, same problem with less debugging, and no answer to the problem other than 'don't use the using statement' which is a bad suggestion.  So for a work around I wrote a Co

How to make linq Object Serializable... 'System.Data.EnumerableRowCollection is not marked as Seriz


Dear All,

Is it possible to make the linq object as serializable. I need to pass the serialized Data as parameter to the Remoting Method.

My LinQ Query is as follows

 Dim LinqQuery = From R In dsEdit.Tables(0) _

                    Where R.Field(Of Integer)("ID") < 400 _

                    Select R


The Definition of ZipObject is as follows.



  Public Function ZipObject(ByVal obj As Object) As Byte()

        Dim ms

Read CSV using Linq - System.IndexOutOfRangeException


I'm trying to get data from a CSV file and add it directly into a database using Linq.

I'm using the following code:

Dim dataImport = From line As String In File.ReadLines("c:\temp\test.csv").Skip(1)
		Let CR = line.Split(Chr(124)) _
		Select New [property] With { _
		.ADDRESS_1 = CR(0), _
		.ADDRESS_2 = CR(1), _
		.ADDRESS_3 = CR(2) _

The code works if I remove all the but the first property e.g.

Dim dataImport = From line As String In File.ReadLines("c:\temp\test.csv").Skip(1)
		Let CR = line.Split(Chr(124)) _
		Select New [property] With { _
		.ADDRESS_1 = CR(0) _

But then I get the old "System.IndexOutOfRangeException: Index was outside the bounds of the array." exception when adding more columns. 

I really want to get this working...

Any ideas?

Linq to SQL does not implement System.Collections.IEnumerable


I am having a lot of the same issue as this poster did with errors on initializing type.

I am trying to learn linq and am about ready to give up - don't get how this is easier...Yell

I'm trying to do a simple update to product names in the product table based on the manufacturer ID

In order to do this I am looping through each record.  I was initally successful with changing all names in the column but when I try to query the data, I continually run into multiple problems.


The following poster has had the same issue I have but his solution isn't working for me. 


I keep getting the same error on the opening bracket on the select statement in the SelectByManufacturerID function...

Error 1 Cannot initialize type 'DataBaseDataManagement.ProductName' with a collection initializer because it does not implement 'System.Collections.IEnumerable'


Button that does the actual updating...

        protected void btnQuery_Click(object sender

No mapping exists from object type System.Xml.Linq.XDocument to a known managed provider native type



I am using XML in getting client data in 3-tier architecture, using XDocument in passing XML among layers.

But when I use it in inserting this XML in XDocument, it throws the following error:

No mapping exists from object type System.Xml.Linq.XDocument to a known managed provider native type. 

I am using the following code, it throws the exception at the bold line:

 public void DALInsertXML(XDocument x)
            SqlConnection connection = new SqlConnection(connection1);
            _command = ne

Playing with Linq grouping: GroupByMany?

One of its features is grouping. Many people understand grouping like it is defined in Sql. Linq is implementing grouping quite the same way. Let's discover this syntax and how to make consecutive groups easier.

LINQ : Implementing IN and NOT IN

I got tried of typing

var result = from s in source
where items.Contains(s)
select s;and so I implemented the IN and NOT IN methods as extension methods:

101 LINQ Samples Tutorials

101 LINQ Example with sample code snippets....This sample prints each element of an input integer array whose value is less than 5. The sample uses a query expression to create a new sequence of integers and then iterates over each element in the sequence, printing its value

Linq: how to share parameters between lambda expressions ?

When using Linq to objects, you will quickly feel the need to pass some parameters from a method to another but it's not so easy because each Linq method is not calling the following one. In a Linq sequence, each method is using the result computed by the previous one. So, local contexts are not visible from one method to another.
The compiler is using two technical different ways to let parameters go out of a method.

Linq to Sql CompiledQuery container

Ok now let's go. Here is just a little trick but with some interesting patterns that could be useful in some other contexts not connected to Linq to Sql.

When using Linq expressions, like with Linq to Sql, translating the expression into something else (sql for example) is taking time and resources. Sometimes it's negligible, sometimes not...
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend