The online racing simulator
.Net - LinqToLfsWorld - a custom LINQ provider for LfsWorld
Hello programmers,

I've just released the first full version of LinqToLfsWorld, which is a custom LINQ implementation used to query the LfsWorld Pubstat service through the .Net Framework.

If you're not quite sure what LINQ is or does, this is a good place to start. Essentially it allows you to write a Language INtegrated Query to transform one set of information into another (such as a url).

What this means is, using .Net and my library, you can query LfsWorld like so (in C#):

var query = from stats in LfsWorldContext.RacerStats
where stats.RacerName == "elkdanger"
select stats;

This query will produce the appropriate url to send to LfsWorld and retrieve the racer's stats for you.

All of the available actions have been implemented according to v1.4 of the pubstat service, and all of the required and optional parameters have been implemented.

Another goal of the project has been to implement a caching system where you can easily cache your responses to save hitting the pubstat service too often, without having to write special code yourself. It also prevents you from hitting the 5-second tarpit if you're not a premium member; of course you can switch this off if you are a premium member.

The library also allows you to use a custom configuration section to store all your LinqToLfsWorld settings in the application configuration file (app.config or web.config). Included with the release download (link below) is an example website which shows you how to use this configuration section.

For the moment, rather than pre-empt any questions that arise on how to use it, if you post here I can answer questions as they come up (assuming someone tries this out of course).

So here's the good stuff. The library comes as a DLL which you reference into your .Net project, and from there you can get access to the LfsWorldContext (the hub for making your queries). Also contained within the download archive is full class library documentation.

You can grab the library on the LinqToLfsWorld project site. Included with the download is an example website which shows you how to use the library to perform a hosts query and how to use the custom configuration section. All the source code is available from the Source Code tab on that site too. I'm also going to provide some code samples and issues with the library on my blog at stevescodingblog.co.uk over the next week or so.

Here's the code from the sample website included which gives you an idea of what you can do with it:

using (LfsWorldContext context = new LfsWorldContext())
{

// Make sure caching is enabled
context.CachingEnabled = true;

// Handle any "request made" events
context.RequestMade += new LfsRequestHandler(context_RequestMade);

/* Make an initial query to the hosts action */
var query = from host in context.Hosts
orderby host.RacerCount descending
select new
{
host.RacerCount,
host.TrackName,
host.HostName,
host.QualifyingMinutes,
host.Laps
};

// Refine further - only show active hosts
var list = query.Where(host => host.RacerCount > 0).ToList();

// Bind to the grid view
LfsHostsGridView.DataSource = list;
LfsHostsGridView.DataBind();
}

Links
Nice proof of concept with LINQ, I've been wanting to give it a go for some time now.

I'll try to test this when I have time - may be quite some time from now though.
Quote from BurnOut69 :Nice proof of concept with LINQ, I've been wanting to give it a go for some time now.

I'll try to test this when I have time - may be quite some time from now though.

No probs - thanks for having a look. And if you get a chance to have a go, let me know what you think
Wow, very nice work!
Hi

Just to let you know I've updated my blog with a fairly lengthy post giving more details about the inner workings of LinqToLfsWorld, and more importantly how to use and configure it!

You can read it here
Hello,

Just to let you know v1.0 of LinqToLfsWorld has now been released! You can grab it from the project homepage.

The interface has essentially stayed the same since the 0.8 release at the beggining of the month, but the main difference is the expression engine which is used to translate your initial queries into a pubstat query; it's a lot more expressive and is a lot less fickle to work with.

My blog still has some discussion on using the library, and this week I plan to write up a bit more about exactly what changes have gone on since the preview release, and some more code examples.

For now, enjoy using it and if there are any issues with the library please email me, contact me on my blog or raise an issue on the Codeplex project page.
Nice work, just the thing I was looking for earlier

D/L'ing
Quote from dougie-lampkin :Nice work, just the thing I was looking for earlier

D/L'ing

Thanks bud. Let us know if you have any issues with the library
Hello,

I've written up a reference which shows all the queries you can make and what parameters you need; you can read it on my blog :-)

Also, if anyone is after some VB.Net samples of these queries, please ask and I'll sort some out.
Hi

I've just released LinqToLfsWorld v1.0.2, which has a couple of minor bug fixes, support for Xml and Json serialization/deserialization and some helper constants for working with Car, Track ident and Steering type parameters.

You can grab it on the 1.0.2 release page.
I know this thread is ages old, but I'd still like to thank you for this one.. I was almost going to try this myself before I did a quick search on the forum.. Very useful, thanks!
No problem - let me know if you have any issues with the library :-)
Quote from elkdanger :No problem - let me know if you have any issues with the library :-)

Thanks! I have one issue.. the library documentation file doesn't seem to work here (see attached). I have IE8 installed, might have anything to do with that? Any ideas?
Attached images
navigation_cancelled.jpg
Quote from traxxion :Thanks! I have one issue.. the library documentation file doesn't seem to work here (see attached). I have IE8 installed, might have anything to do with that? Any ideas?

Doesn't work for me and I'm using XP with an IE6 engine still.
Quote from traxxion :Thanks! I have one issue.. the library documentation file doesn't seem to work here (see attached). I have IE8 installed, might have anything to do with that? Any ideas?

Hmm not sure about this one. I've had something similar when trying to view .chm files from a network drive.

Also, have a look here and let me know if this works for you.
Quote from traxxion :Great! That did the trick! Many thanks for your quick reply!!

Hehe no probs; CHM help docs can be a bit of a pain sometimes
Hi again

I have a problem when requesting team data:
var memberList = (from team in context.Teams
where team.Name == "F1RST Racing"
select team.Members).ToList();

The request above raises the following exception (Anonymous Select not supported):

LinqToLfsWorld.PubstatQueryException was unhandled by user code
Message="Anonymous Select not supported"
Source="LinqToLfsWorld"
StackTrace:
bij LinqToLfsWorld.Queries.LfsWorldQuery`1.GetBaseExpression(Expression expression) in C:\Documents and Settings\René\Mijn documenten\Visual Studio 2008\Projects\LinqToLfsWorld\Queries\LfsWorldQuery.cs:regel 214
bij LinqToLfsWorld.Queries.LfsWorldQuery`1.GetQueryText(Expression expression) in C:\Documents and Settings\René\Mijn documenten\Visual Studio 2008\Projects\LinqToLfsWorld\Queries\LfsWorldQuery.cs:regel 87
bij LinqToLfsWorld.Queries.LfsWorldQuery`1.Execute(Expression expression) in C:\Documents and Settings\René\Mijn documenten\Visual Studio 2008\Projects\LinqToLfsWorld\Queries\LfsWorldQuery.cs:regel 116
bij LinqToLfsWorld.Linq.Query`1.GetEnumerator() in C:\Documents and Settings\René\Mijn documenten\Visual Studio 2008\Projects\LinqToLfsWorld\Linq\Query.cs:regel 70
bij System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
bij System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
bij TalentScout.MainForm.UpdateTeam() in C:\Documents and Settings\René\Mijn documenten\Visual Studio 2008\Projects\TalentScout\TalentScout\MainForm.cs:regel 301
bij TalentScout.MainForm.ExecuteRequest(LfsWorldRequest req) in C:\Documents and Settings\René\Mijn documenten\Visual Studio 2008\Projects\TalentScout\TalentScout\MainForm.cs:regel 256
bij TalentScout.MainForm.tmrRequest_Elapsed(Object sender, ElapsedEventArgs e) in C:\Documents and Settings\René\Mijn documenten\Visual Studio 2008\Projects\TalentScout\TalentScout\MainForm.cs:regel 228
bij System.Timers.Timer.MyTimerCallback(Object state)
InnerException:

Now I just noticed the TeamQuery class, but I can't seem to get that work.. would you mind posting a quick example, like the RacerStats one in your first post? Would be very appreciated!

Cheers!
Hi there,

A query like that won't quite work unfortunately; this is because the pubstat service itself does not support selecting in that way; it doesn't even support searching by team name.

The way to get around it is to essentially split it into two separate queries, like:


var query = (from team in context.Teams select team).ToList();
var query2 = (from team in query where team.Name == "F1RST Racing" select team.Members);

Or this following sample may also work (I can't test it at the moment), but give it a shot:


// Almost the same as your query, but note the placement of the call to ToList()
var memberList = from team in context.Teams.ToList()
where team.Name == "F1RST Racing"
select team.Members;

The "work-around" in both samples above is the call to ToList(), which of course evaluates and executes the query before your extra query parameters are added in to the mix. The difference is after the ToList() call you are performing your query on the results in memory, rather than trying to send to the pubstat service which is why the error is occuring.

Let me know how you get on with that

Edit: Since you mentioned the racer sample from my first post, I seem to remember something funny going on with that sample and that actually may not be the correct code, because I don't think you can actually perform a select like that without evaluating the result set first. I'll have a look later and update the code with the correct version if it doesn't in fact work.
Took me some time since I spent the weekend abroad, but I've just tried your suggestions!
Quote from elkdanger :

// Almost the same as your query, but note the placement of the call to ToList()
var memberList = from team in context.Teams.ToList()
where team.Name == "F1RST Racing"
select team.Members;

The "work-around" in both samples above is the call to ToList(), which of course evaluates and executes the query before your extra query parameters are added in to the mix. The difference is after the ToList() call you are performing your query on the results in memory, rather than trying to send to the pubstat service which is why the error is occuring.
Let me know how you get on with that

Perfect, that works, and thanks to your explanation I also know why; much appreciated, thanks a lot!!

Quote from elkdanger :Edit: Since you mentioned the racer sample from my first post, I seem to remember something funny going on with that sample and that actually may not be the correct code, because I don't think you can actually perform a select like that without evaluating the result set first. I'll have a look later and update the code with the correct version if it doesn't in fact work.

I've implemented the racerstats request according to your example and so far I've had no problems with it.. but if you think something's wrong, I'd appreciate hearing your suggestion

Thanks again!
No problems mate

FGED GREDG RDFGDR GSFDG