1

how it's possible to translate

var vehiclequery = db.position
    .GroupBy(c => c.device_id)
    .Select(g => g.OrderByDescending(c => c.sendtime).FirstOrDefault())
    .Select(c => new myPosition()                                                          
    {
        battery_percentage = c.battery_percentage,
        device_id = c.device_id,
        latitude = c.latitude,
        longitude = c.longitude,
        speed = c.speed,
        sendtime = c.sendtime
    });

to query syntax? Now I have just something stupid and I have no idea how to make it work. It's something like this?

var vehiclequery = from dPosition in db.position
                   group dPosition by dPosition.device_id into xx
                   select new
                   {
                       device_id = xx.device_id
                   };

I know there are lot of things missing but I'm stuck at this point. I tried tool that was reccomended here on stack - http://www.linqpad.net/, but this only translate from query syntax to method syntax.

Thanks for any help or leads how to make this work. Some useful manual pages would be also very appreciated, now I'm using just http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

4
  • 2
    Always possible! But you are trying to convert LINQ to another form of LINQ, but not to the methods syntax. Commented Mar 28, 2014 at 22:39
  • 1
    Just to clarify terms you are translating from LINQ method syntax to LINQ query syntax. Like @Dmitry said this is always possible... sometimes challenging but definitely possible. Commented Mar 28, 2014 at 22:40
  • thanks, yes of course I was mistaken. sorry - I updated post Commented Mar 28, 2014 at 22:42
  • Resharper does a good job of converting from QUERY to METHOD syntax and the other way round. Commented Mar 29, 2014 at 6:38

1 Answer 1

4

It's not a 1-to-1 transcription, because of how let works (you still have access to xx after let), but will produce the same results:

var vehiclequery = from dPosition in db.position
                   group dPosition by dPosition.device_id into xx
                   let c = xx.OrderByDescending(x => x.sendtime).FirstOrDefault()
                   select new
                   {
                       battery_percentage = c.battery_percentage,
                       device_id = c.device_id,
                       latitude = c.latitude,
                       longitude = c.longitude,
                       speed = c.speed,
                       sendtime = c.sendtime
                   };

or with sub query as a syntax query:

var vehiclequery = from dPosition in db.position
                   group dPosition by dPosition.device_id into xx
                   let c = (from x in xx
                            orderby x.sendtime desc
                            select x).FirstOrDefault()
                   select new
                   {
                       battery_percentage = c.battery_percentage,
                       device_id = c.device_id,
                       latitude = c.latitude,
                       longitude = c.longitude,
                       speed = c.speed,
                       sendtime = c.sendtime
                   };
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You very much! :) This is working perfectly. And I've never seen "let" clause before - it's not in the Microsoft LINQ samples.

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.