VB Dynamic Query Library woes

According to my searches on the internet, not many people were having trouble trying to use the Linq Dynamic Query library inside a class library project.

The problem was that with my Asp.Net web project, I wanted to have my middle-tier code inside a class library which would then be referenced by the website (and another planned website later on). This class library would contain (amongst other things) my Linq data context and the Dynamic Query library. However, whenever I added the Dynamic Query library to the project the compiler would start complaining about missing objects.

After much tearing my hair out and research I still couldn’t find the problem so I left it to work on something else. A similar issue arised when I was working on a completely unrelated piece of code, so it wasn’t just attributed to the Linq library in my original problem. The real issue?

VB.Net class library namespaces. When you create a VB.Net class library project, you can specify a default namespace for everything in that library, and so when you are creating something inside a sub-namespace you leave off the default namespace which is already set on the project.

The dynamic library is implemented inside the namespace System.Linq.Dynamic, and so when I added it to my class library project the namespace it was in was actually MyNamespace.System.Linq.Dynamic. This meant that the System namespace now clashed with MyNamespace.System, and the data context which imports the System namespace, and was in MyNamespace, was importing the wrong one. Obviously, this was also happening to anything which imports the System or any sub-namespace of System. Which means essentially that the hellmouth opened and the world was in chaos. This issue doesn’t arise in a normal web project, since you don’t define a default namespace for classes. I haven’t tried this, but I also assume the issue won’t arise in a C# class library project since, while you can specify a default namespace for your classes, the compiler doesn’t anonymously prepend your default namespace name onto the one you’ve explicitely set in the code file.
The solution is simple – dive into the code file for the Linq Dynamic Query library and remove the namespace declaration.

Snippets , ,

7 responses to VB Dynamic Query Library woes


  1. Thanks, big help. So, is this a built in feature, a quirk, or a bug?

  2. THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU Really. Thank you.

  3. Just a quirk to be aware of when working with VB.Net class libraries really. You might be able to remove the default namespace in the properties for the project, which would also solve this problem. Depends on your particular scenario really.

  4. Hi Josh, I am getting the same error as you — "Type 'System.Data.Linq.Mapping.DatabaseAttribute' is not defined" I have been working on this for 2 days, if you find a solution or already have please post or send me an email. I am extremly fustrated and running out of time to complete my project. I would really appreciate any help. Thanks

  5. Another great big THANK YOU! I also had a problem with an error displaying the "Signature" class was not defined after the namespace removal, so I had to remove the mention of the namespace in the "Equals" function Public Overloads Function Equals(ByVal other As Signature) As Boolean Implements IEquatable(Of Signature).Equals

  6. This has been a bear of an app to write. I just finished another site doing almost the same things but this one just is acting weird. I am getting the: Type 'System.Data.Linq.Mapping.DatabaseAttribute' is not defined I have never had this error b4. Very curious. Anyway what part of the DBML generated code did you remove? Here's mine: Option Strict On Option Explicit On Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Data.Linq Imports System.Data.Linq.Mapping Imports System.Linq Imports System.Linq.Expressions Imports System.Reflection _ Partial Public Class Troop533EventsDataContext Inherits System.Data.Linq.DataContext Private Shared mappingSource As System.Data.Linq.Mapping.MappingSource = New AttributeMappingSource Any help would be appreciated. Contact me at this email address if you wouldn't mind helping. Thanks in advance. youngboy1606 AT ya hoo.com

  7. Hmm to be honest it sounds like you might have a different problem to what is described here. For a start, I didn't modify anything in my generated .dbml file. If you've got the Dynamic Query library included in your project like I have, then you may have the same problem with conflicting namespaces in Vb.Net; otherwise, I'm not sure what it could be.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>