I am trying to parse some simple XML like this:
XElement thisLevel = from l in xmlElements.Descendants("Level")
where l.Element("LevelNum") == thisLevel
select l;
But I get an error on "levels" saying:
Could not find an implementation of the query pattern for source type
'System.Collections.Generic.IEnumerable'.
'Where' not found. Are you missing a reference or a using directive
for 'System.Linq'?
Strangely I can grab descendents:
var levels = xmlElements.Descendants("Level");
That works, but I can't seem to where on it.
As ChrisF mentioned, these using statements are both required:
using System.Xml.Linq;
using System.Linq; // this one was missing
I had thought System.Xml.Linq would cover all everything linq-related.
Thanks, ChrisF!
Related
I've been trying to replicate the "A simple example" posted at https://dash-julia.plotly.com/clientside-callbacks
The server runs... but when I connect to it I get a JSON parse error in Firefox.
I was able to solve the problem, but I'd like to understand what's wrong...
The problem was this line inside the Dash app.layout:
options=[(label = country, value = country) for country in available_countries]
and the available_countries variable was obatined from:
read_remote_csv(url) = DataFrame(CSV.File(HTTP.get(url).body))
df = read_remote_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv")
available_countries = unique(df.country)
Apparently the error showed because available_countries was an Array{String31,1}: and specifically the problem was the String31 type.
when I converted the variable to a plain String type:
available_countries = convert(Array{String,1},available_countries)
the problem solved.
Now... I'm not sure if the String31 type came from the HTTP.get(), CSV.File() or the DataFrame() functions.
I'm assuming the example used to work when it was originally written but it broke with an update...
Can anyone explain where exactly the error originates? Is it a Package version thing? which package? (HTTP, CSV, Dataframes)? How can I avoid it moving on?
I know this question was asked before but I couldn't find any solution for my issue.
I am developing a WebAPI with more than 10 Controllers which their methods access a server DB.
I am using Linq2SQL to write the queries and using Json to serialize the return to send it back to my application.
The problem is no matter how simple is the query it returns the self reference loop when serialize and this is happening in all controller methods. See one example below:
var retitems = dtcxapi.ListItems.AsQueryable()
.Where(i => i.IsActive == true && i.ListName.ToLower() == listName.ToLower()).ToList();
where dtcxapi is my DataContext and ListItems is my table.
When serialize it shows: Self referencing loop detected with type 'BV.IMSWEBAPI.User'. Path '[0].User1.Users1'.
But as I said this error will occur for any query in any controller methods. I tried already to use the ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore in mWeb config but it didn't fix.
Any help will be really appreciated.
Thanks
The only true way to fix this is to not return your Linq objects and instead return a DTO/Model that is not tied to your database. If you are returning your database objects, you will always run into self referencing loops because of Navigation properties.
You haven't mentioned if you are using .NET Core or .NET Framework, but if it's Core, it won't use web.config at all and instead you should modify your startup method :
services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
But again, this is a bandaid, the correct solution is to use DTOs
More info : https://dotnetcoretutorials.com/2020/03/15/fixing-json-self-referencing-loop-exceptions/
In jsonschema validation, I am looking to print all the Validationerror of several instances with several respective schemas in one single for loop as opposed to one for loop for one round of validation. I want to do something like this:
validator = jsonschema.Draft4Validator(schema1)
errors1 = validator.iter_errors(instance1)
validator = jsonschema.Draft4Validator(schema2)
errors2 = validator.iter_errors(instance2)
for error in errors1 + errors2:
print error.message
But, I can't do errors1 + errors2 as they are of type generator. Is there a way to add all the errors into one single iterable so I can use just one for loop? Any help appreciated. Thanks.
Similar question is here How to join two generators in Python?
You have to use itertools.chain() function to join generators
for error in itertools.chain(errors1, errors2):
print error.message
Background
I'm trying to create a function componentFromJson that can reconstitute a component graph from JSON. I took a simple approach where I'm using getMetaData in order to lookup the component properties to instantiate the right types.
The function would be used like: comp = componentFromJson(json, 'RootComponentType')
Problem
The problem is that the type of the properties are not necessarily fully qualified because namespaces may have been imported, as we can see below.
<cfimport path="some.namespace.Test">
<cfcomponent>
<cfproperty name="test" type="Test">
</cfcomponent>
When I'm trying to do createObject('Test') from the componentFromJson function context it's obviously failing because the calling context doesn't have the imports.
I have tried many different ways to resolve the problem, including temporarily defining the component factory function on the parent component dynamically and using invoke to call the factory function in the context of the parent CFC, but it doesn't work.
E.g.
<cfscript>
parentCmp = createObject('SomeCmp');
parentCmp.createComponent = function (type) {
return createObject(type);
};
childCmp = invoke(parentCmp, 'createComponent', { type = 'Test' });
</cfscript>
Horrible solution
The only way I can think of solving this issue right now is to parse the ColdFusion code of the CFC and extract the import statements, but I'm expecting this to be way too slow for the purpose. Not only that but this wouldn't cover all edge cases.
Ideas?
I'd like to know if someone has a better idea for solving this issue? Is there an entirely different approach I could take? There is probably a way to do this using the ColdFusion runtime classes, but I haven't figured it out yet.
Well, it turns out that it wasn't so hard when you knew the underlying mechanics of the ColdFusion runtime (which I had trouble to find initially).
I finally discovered that a ColdFusion component, which is represented as a coldfusion.runtime.TemplateProxy was encapsulating a coldfusion.runtime.CFPage instance which in turns has a createObject method.
Therefore, here's the solution I came up with using Java reflection:
<cfset host = new SomeComponent()>
<cfset pageField = createObject('java', 'coldfusion.runtime.TemplateProxy').getClass().getDeclaredField('page')>
<cfset pageField.setAccessible(true)>
<cfset page = pageField.get(host)>
<cfset test = page.createObject('Test')>
Has anyone ever used this and got it working? I'm having real problems because it's telling me there is a JScript error and I'm not sure how to fix it.
Links on this are here to get some background on the product, but it's a JSON parser for classic asp.
Iterating though a JSON return using ASP Xtreme Evolution
I'm sure I had this working the other other day, but now I've come back to it, it's throwing a JScript error, and I can't seem to make it work now.
So, I'm posting JSON to a classic asp page, this is the code I'm using to include the json2.asp page as instructed in all the tutorials:
<%#LANGUAGE="VBSCRIPT"%>
<!--#include file="json2.asp"-->"%>
<% 'json processing details here..
The error it throws is on a line of json2.asp and it's a JScript error. Here is the exact error:
Microsoft JScript compilation error '800a03ea'
Line 765 json2.asp
I believe this is a version of Crockfords json2.js and I think the reason it's in an asp page is so it can use JScript, but it's not progressing for me. Anyone have any idea what I'm talking about that could help me out before I tear all my hair out?!
Let me know if you need more info - many thanks
UPDATE
Here is line 765 (in the middle between the comments)
if (/^[\],:{}\s]*$/.
test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '#').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
// In the third stage we use the eval function to compile the text into a
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
// in JavaScript: it can begin a block or an object literal. We wrap the text
// in parens to eliminate the ambiguity.
j = eval('(' + text + ')');
// In the optional fourth stage, we recursively walk the new structure, passing
// each name/value pair to a reviver function for possible transformation.
return typeof reviver === 'function' ?
walk({'': j}, '') : j;
}
You're not including the right things. You need the whole library available and to include the following files (change your paths appropriately).
<!-- #include file="includes/extlib/axe/base.asp" -->
<!-- #include file="includes/extlib/axe/classes/Parsers/json.class.asp" -->
Then to actually use, you do something like this:
set oJson = new Json
oJson.loadJson(strJSON)
strSomething = oJson.getElement("theElement")
set oJson = nothing
All this taken from working code.