c# - Linq Enumeration Error -


i getting users posted form data, might empty.

var users = request.form.getvalues("users[]") == null ? null : request.form.getvalues("users[]").toarray(); 

so, users becomes null if empty. but, following linq query not work.

 var submissions = db.quicksearchviews.where(x => (users != null && users.contains(x.userid)))                                       .tolist(); 

error:

additional information: unable create null constant value of type 'system.string[]'. entity types, enumeration types or primitive types supported in context.

i going assume entity framework based on error message. reason why query throws error message (you didn't post full error & stack trace, it's notsupportedexception) twofold

  1. users not entity within context db (database mapping) , checking if null cannot translated sql statement.

  2. users.contains(x.userid) cannot translated sql statement because users not part of database.

you'd need reorder way written:

var users = request.form.getvalues("users[]") == null ? null : request.form.getvalues("users[]").toarray(); if(users != null) {      list<quicksearchview> qsvs = db.quickseachviews.tolist();      var submississions = qsvs.where(x => users.any(x.userid)); } else {       //something when users null } 

potentially, might work well, haven't tested , don't remember since haven't tried recently:

var submissions = db.quicksearchviews.where(x => users.any(x.userid)); 

you might need call db.quicksearchviews.load(); first though, you'll need test it.

pro tip:

if you're concerned users being null can use null coalescing operator ??. since mentioned users null when empty can this:

var users = (request.form.getvalues("users[]") ?? new string[0]).tolist(); 

this make sure users never null, , therefore don't need check it. however, don't need call tolist() on or in original post since request.form.getvalues() returns array (string[]), implements ienumerable<t> access linq extension methods.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -