2

I need to select two fields out of three fields from Json data using Linq.js

Required output should be

[{ "A": -27, C: "country 1" } , { "A": 28 , C: "country 2"} ] 

using "linq.js" from following path: [ https://raw.github.com/gist/1175460/fb7404d46cab20e31601740ab8b35d99a584f941/linq.js ]

Sample data

var Data = [{ "A": -27, "B": -39, C: "country 1" }, { "A": 28, "B": 0 , C: "country 2"}]

var filter = "  x =>    x['A'], x['C']  ";
var findItem = Enumerable.From(Data)
.Select(filter)
.ToArray();

console.log(findItem);

code at JsFiddle : http://jsfiddle.net/gLXNw/9/

2 Answers 2

7

Your "lambda" function must return a valid java object.

Your query should be more like this:

var query = Enumerable.From(data)
    .Select("x => { A: x['A'], X: x['C'] }") // object initializer
    .ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

and thanks but in .net linq i used to select fields using cards.Select(c => new { c.Type, c.Limit }).ToList() ;
I change the select clause as follows cz i am creating select using run time values. "x => { \"A"\: x['A'], \"C"\: x['C'] }"
7

You can use function in result selector instead of lambda (if lambda expression was used not for political reasons :))

var findItem = Enumerable.From(Data)
.Select(function(x){
    return {
        'A': x['A'], 
        'X': x['C']
    };
}).ToArray();

1 Comment

it works if property contain space e.g "Hair Cut" where x.Hair Cut will not work, saved my time, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.