31492

“Argument expression is not valid” error with EF4 Linq sub select query

Question:

Any ideas as to why this query compiles, but then throws this runtime error:

<blockquote>

Argument expression is not valid

</blockquote>

I know I could change my db model, but in this case it's not possible. Any ideas how to get something like this working? Not even sure what this would be called. Thanks.

DBContext db = new DBContext(); var books = (from b in db.BOOKS select new { b.ID, b.NAME, AuthorName = db.PEOPLEs.Where(p=>p.ID==b.AUTHOR).First().USER_ID, }).ToList();

Answer1:

I've found I have the best luck with complex inner queries by using let expressions. This does a subselect and allows you greater flexibility to bind an element from a subselect. HOWEVER, notice that I am only doing a First() on the author assignment in the anonymous object. This is because if you do a First().PropertyName and First yields a null value it will blow up.

Good luck and double check syntax. I don't have your full object set so I cannot generate a fully working demo, however, this was tested with an object tree that I have on one of my own projects.

var books = ( from b in db.BOOKs let author = (from a in db.PEOPLEs where b.AUTHOR == a.ID select a) select new { ID = b.ID, NAME = b.Name, Author = author.First() } ).ToList(); foreach(var x in books) { string AuthorName = x.Author.USER_ID; //Do other stuff }

Answer2:

If you have a look on your code, you have

DBContext db = new DBContext(); var books = (from b in db.BOOKS select new { b.ID, b.NAME, AuthorName = db.PEOPLEs.Where(p=>p.ID==b.AUTHOR).First().USER_ID, <<-- Here coma }).ToList();

It is expecting another parameter there, just remove it and it sould pass

:)

Answer3:

So, the final solution was:

var books = ( from b in db.BOOKs let author = (from a in db.PEOPLEs where b.AUTHOR == a.ID select a) select new { ID = b.ID, NAME = b.Name, Author = author.First().USER_ID } ).ToList();

Thanks!

Answer4:

Anyway, First<T>() also have an <a href="http://msdn.microsoft.com/en-us/library/bb535050.aspx" rel="nofollow">overload</a> First<T>(Predicate<T>), i.e.:

AuthorName = db.PEOPLEs.First(p=>p.ID==b.AUTHOR).USER_ID <hr />

You can use LINQ in methods styles:

var books = (from b in db.BOOKS let author = db.PEOPLEs.Where(p => p.ID == b.AUTHOR).First() select new { b.ID, b.NAME, AuthorName = author.USER_ID, }).ToList();

Recommend

  • How to change the query to give last 15 weeks of data instead of last 15 days from the SQL Server
  • Centralized rollback-for using @transactional
  • Hql, How to write join query between tables that has one to many relationship?
  • Finder in Liferay's ServiceBuilder
  • rounding answers to decimal places [duplicate]
  • Using LINQ to XML to Process XML in Multiple Namespaces
  • How to get email address along with people list in google plus API
  • SharePoint TextField ClientID changes?
  • ObjectMaterialize in EF not firing on first level query
  • How to read data from a text file if the file location is not known in c#?
  • Mongoose TypeError: Cannot use 'in' operator to search for '_id' in
  • View/Download Pdf Files in React - Router 4
  • Reloading table causes flickering
  • How to bind comma separated list of values to List
  • ASP.NET, C# How to Pass a StringQuery to a custom SQL Command
  • Can you build a truly RESTful service that takes many parameters?
  • Stitching 2 images (OpenCV)
  • TSQL Rolling Average of Time Groupings
  • How to Add Polymorphic Comments to Feed?
  • Are there any side effects from calling SQLAlchemy flush() within code?
  • Cypher - matching two different possible paths and return both
  • MySQL performance when updating row with FK
  • NHibernate manually control fetching
  • User messaging system
  • xcode don't localize specific strings
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Display images in Django
  • Word Open XML Mail Merge
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Is possible to count alias result on mysql
  • Calling of Constructors in a Java
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?