I am wondering if it is possible to use LINQ with NLua? I have searched various sources including the project's github page, but without any luck. I got the following minimal C# example:
using (var lua = new Lua())
{
lua.LoadCLRPackage();
lua["data"] = new[] { 1, 2, 3, 4, 5 };
var res = lua.DoFile(@"C:\..\MyLua.lua");
}
MyLua.lua
looks like this:
luanet.import_type('System.Linq')
-- syntax error near 'in'
res = from d in data where d > 2 select d
-- ')' expected near '='
res = data:Where(x => x > 2)
return res
When executed, the syntax errors (shown as comments in the lua script above) are returned by NLua. It would be nice to know if this is by design and if so, if any future version of NLua is going to support LINQ?
LINQ is a "language integrated query": integrated is key here, i.e. C# syntax supports constructs that adhere to LINQ syntax and semantics.
NLua does not change the syntax of Lua (let alone the semantics), it simply makes the .NET libraries available from Lua, makes it easy to run Lua script from C#, and makes it easy to exchange data between the two languages. So is it by design that your LINQ query is not supported in NLua? Well, sort of! In that NLua by design does not change or extend Lua syntax.
Will any future version support it? Well, any future is a mighty long time, and who can predict the future? Even if the current maintainers say no, they could decide to drop the project in a few months, have it taken over by another developer who decides this would be a great idea. So that part of your question doesn't have an answer.
So all that being said, the answer is still yes, you can use LINQ from Lua: create your LINQ queries in C# functions which you publish to Lua, and call them from Lua. Admitedly, this is not bad but still not as powerful as what you probably want to do, which is to create LINQ in your Lua code without need to compile C# code. I don't think this is possible, although I vaguely remember there is a way to compile C# code on the fly but I could be dreaming.