1

I have this in javascript and it evaluates as expected to a string representing a number (valid one):

dt2.Rows[0]["CountryID"]

I was wondering if I can combine this in javascript with linq something like dt2.Rows.Select (x => x.CountryID) so I can retrieve all country ids at once. But I get a syntax error. What is the correct syntax for linq in javascript (if it exists).

Sami
  • 393
  • 8
  • 22
  • 3
    LINQ is a feature of C# how will it ever work in Javascript – Ipsit Gaur Nov 22 '17 at 08:30
  • 1
    You can use `map` in place of `Select`. – Daniel Kelley Nov 22 '17 at 08:30
  • 1
    You can use jslinq for javascript. Refer https://jslinq.codeplex.com/ – Hameed Syed Nov 22 '17 at 08:31
  • 1
    No, it´s a .NET-feature. Javscript is of course not a .NET-language, thus no LINQ. However you can of course build your own methods as done within the project linked by Hameed. However you can of course make a server-request which sends you the information. – MakePeaceGreatAgain Nov 22 '17 at 08:36
  • `dt2.Rows.map(function (x) { return x.CountryId; })` – poke Nov 22 '17 at 08:39
  • Modern browsers supports so called arrow functions, so you can make it even closer without additional libs `dt2.Rows.map(x=>x.CountryId)` – ASpirin Nov 22 '17 at 08:44
  • @ASpirin If OP attempted to use `dt.Rows.Select(x => x.CountryID)` and that caused a syntax error, then they are using a browser that does not support arrow functions yet since that’s the only *syntax* that can fail there. – poke Nov 22 '17 at 08:48
  • I believe LInQ is now a real possibility for Javascript. I did some work in that direction and you can get it all from here: https://siderite.dev/blog/linq-in-javascript-linqer. Note that the linq.js library that was proposed as an answer uses a different approach and is way more complex for at most the same results. – Siderite Zackwehdex Jan 06 '20 at 16:09

2 Answers2

3

LINQ is part of C#, not Javascript. However, there are some libraries (like LINQ.JS - here is an example) that will recreate this.

To get the same effect in JS, just use a loop? Like so:

var idList = new Array(dt2.Rows.length);
for(var index = 0; index < dt.Rows.length; index++){
        idList[index] = dt2.Rows[index].CountryId;
}
CodeGhost
  • 311
  • 1
  • 11
2

You could take linq.js with the following syntax for getting an array with values of CountryID.

Methods for getting a value

'$.CountryID'                        // short syntax with $ as actual row
'x => x.CountryID'                   // lambda as string
x => x.CountryID                     // lambda as ES6 native arrow function
function (x) { return x.CountryID; } // standard JS function

var array = [{ id: 1, CountryID: 2 }, { id: 2, CountryID: 1 }, { id: 3, CountryID: 3 }, { id: 4, CountryID: 2 }, { id: 5, CountryID: 2 }],
    result = Enumerable
        .From(array)
        .Select('$.CountryID')
        .ToArray();
   
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>
Community
  • 1
  • 1
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392