2

I'm new to LINQ query and I want to get 1 item (the one with id = 0) out of a JToken but I can't find out how this should be working.

I tried a lot a different way but this the code I tried first :

var statId0 = from stat in objectRankedStats where (int)stat["id"] == 0 select stat;

I've got this error :

Could not find an implementation of the query pattern for source type 'Newtonsoft.Json.Linq.JToken'. 'Where' not found. Are you missing a reference or a using directive for 'System.Linq'?

This is what looks like objectRankedStats :

{[  {
    "id": 40,
    "stats": 
    {
        "totalSessionsPlayed": 10,
        "totalSessionsLost": 8,
        "totalSessionsWon": 2,
    }  
},
{    
    "id": 6,
    "stats":
    {      
        "totalSessionsPlayed": 3,
        "totalSessionsLost": 2,
        "totalSessionsWon": 1, 
    }  
}

]}

I don't understand how the "quotes" handle backspaces

I declared objectRankedStats like this and I use it somewhere else in my code and it works.

var objectRankedStats = JObject.Parse(output)["champions"];

This is why I think it's null : enter image description here

I made a simplify version of the code I'm using on dotnetFiddle.com : https://dotnetfiddle.net/yS5cTk

MHogge
  • 5,408
  • 15
  • 61
  • 104
  • 4
    As the error said, have you put `using System.Linq;` at the header yet? – tia Dec 08 '14 at 16:56
  • 3
    That isn't valid JSON. Did you mean to include a name for the array? Or are the outer `{` and `}` mistakes? – Andrew Whitaker Dec 08 '14 at 16:58
  • @tia : Oh damn ! I was used to the typical error message "The type or namespace could not be found" so I didn't think of it. The using was missing but now it returns null (or I'm just to dumb to access the information). – MHogge Dec 09 '14 at 10:02
  • @Andrew Whitaker : This JSON is valid 'cause it comes from an official API but I "hardcoded" this part of the JSON file that was too big and the name of the array is "champions". But thank you both for your comments. – MHogge Dec 09 '14 at 10:03
  • It shouldn't return null. It returns empty list or a list with single null item? How did you check that it's null? – tia Dec 09 '14 at 12:58
  • Can you show a complete example demonstrating your problem, possibly on http://dotnetfiddle.net? – Andrew Whitaker Dec 09 '14 at 15:40
  • This is a simplify version of what I'm doing : https://dotnetfiddle.net/yS5cTk – MHogge Dec 13 '14 at 15:02

1 Answers1

3

This code works for me: https://dotnetfiddle.net/P95aNq

using System;
using System.Linq;
using Newtonsoft.Json.Linq;

namespace ConsoleApplication1
{
    static class Program
    {
        static void Main()
        {
            try
            {
                const string output = @"{""champions"": [  {
    ""id"": 40,
    ""stats"": 
    {
        ""totalSessionsPlayed"": 10,
        ""totalSessionsLost"": 8,
        ""totalSessionsWon"": 2,
    }  
},
{    
    ""id"": 6,
    ""stats"":
    {      
        ""totalSessionsPlayed"": 3,
        ""totalSessionsLost"": 2,
        ""totalSessionsWon"": 1, 
    }  
}
]}";

                var objectRankedStats = JObject.Parse(output)["champions"];

                var champ = objectRankedStats.FirstOrDefault(jt => (int)jt["id"] == 6);

                Console.WriteLine(champ);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}
omikad
  • 979
  • 8
  • 10
  • This is working perfectly ! Could you explain me briefly the difference between var statId0 = from stat in objectRankedStats where (int)stat["id"] == 0 select stat; And var champ = objectRankedStats.FirstOrDefault(jt => (int)jt["id"] == 6); Please? – MHogge Dec 13 '14 at 16:43
  • 1
    Your statId0 is the IEnumerable object. In brief explanation IEnumerable is the interface for collections, sequences. When you use FirstOrDefault you extract first item from the sequence (or null value, of sequence is empty). – omikad Dec 13 '14 at 17:04