1

I am trying to convert the the following SQL statement into a linq query but for some reason I cannot get it to work!!

SELECT     o.ITEMID, COUNT(o.ITEMID) AS COUNT, MAX(i.QUANTITY) AS Quantity
FROM       ORDERS AS o LEFT OUTER JOIN
           INVENTORY AS i ON o.ITEMID = i.ITEMID
           GROUP BY o.ITEMID

I found this link, somebody having a similar problem but I cant seem to apply this to what i need.


thanks for all your help.

This is the code i have so far

Dim castleavailability = _
            From o In orders _
            From i In inventorys _
            Where (x >= o.ITEMID = i.ITEMID)
            Group New With {o, i} By o.ITEMID Into oi()
            Select New With {.ItemId = oi.Key, .Count = oi.Select(y >= y.o.ItemId).Count(), .Quantity = oi.Select(y >= y.i.Quantity).Max()}

the error I am getting now is "Definition of method 'oi' is not accessible in this context." referring to the "group new with" line. Any ideas on how to resolve this

Many Thanks

3
  • What have you tried so far? And "it doesn't work" isn't an error description! Doesn't it compile? Does it throw a runtime exception? Does it run but return wrong results? Commented Mar 7, 2012 at 14:58
  • Is it Linq-To-Sql or Linq-To-Entities? They are not the same thing. Commented Mar 7, 2012 at 15:02
  • Hi sorry it is linq to Entities. I didnt mean to click Linq to SQL Commented Mar 7, 2012 at 15:23

2 Answers 2

4

This should work for you

var query = from o in context.Orders
            from i in context.Inventory
                             .Where(x = > o.ItemId = x.ItemId)
                             .DefaultIfEmpty()
            group new { o, i } by o.ItemId into oi
            select new
            {
              ItemId = oi.Key,
              Count = oi.Select(y => y.o.ItemId).Count(),
              Quantity = oi.Select(y => y.i.Quantity).Max(),
            };
Sign up to request clarification or add additional context in comments.

3 Comments

Wow that was super fast thanks very much i will test this now.
Hi I am coding in vb so have changed this slightly to get rid of the errors showing in VS i have Dim castleavailability = _ From o In orders _ From i In inventorys _ Where (x >= o.ITEMID = x.ItemId) group new { o, i } by o.ItemId into oi select new {ItemId = oi.Key, Count = oi.Select(y => y.o.ItemId).Count(), Quantity = oi.Select(y => y.i.Quantity).Max()} I am getting the error Type or 'with' expected at 'group new { ' Thanks
@user789433: Change every new { to New With { and add a dot in front of the left hand side of the inner expressions, e.g. group New With { o, i } and select New With {.ItemId = oi.Key, .Count = .... See here for more info.
0

You can also use Linqer software to convert sql query to Linq Lambda query.

You can get this software from following link:

http://www.sqltolinq.com/

1 Comment

I'm using Linqer but can't find any choice to translate SQL to Linq using Lambda expression, just classic Linq

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.