views:

23

answers:

1

I've reached the end of my Linq rope. Need your help!

Heres my table structure first(all linq to sql objects):

InventoryItems
  -ID
  -AmtInStock

IventoryKits
  -ID

InventoryKits_to_InventoryItems
  -InventoryItemID
  -InventoryKitID

So i need to do a projection like the following

 var q2=from k in GetAllKits()//returns IQueryable<InventoryKit>
                    select new VMPublication()//ViewModel Object
                   {
                       ID = k.ID,
                       Name = k.Name,
                       WebAmountInStock = ,//need to get the Min() AmtInStock from InventoryItems here
                       ItemCode = k.ItemCode,
                       WebAmountOrdered = k.AmtOrdered.ToString(),
                       WebReminderAmount = "",
                       WebAmountWarning="",
                       Type = "Kit"
                   };

i have no idea how to get that Min() of InventoryItem's AmtInStock in that query.

Please help! Very Appreciated!

A: 

I'm guessing at your association names, but try something like:

var q2=from k in GetAllKits()//returns IQueryable<InventoryKit>
                select new VMPublication()//ViewModel Object
               {
                   ID = k.ID,
                   Name = k.Name,
                   WebAmountInStock = (from i in k.InventoryKits_to_InventoryItems
                                       select i.InventoryItem.AmtInStock).Min(),
                   ItemCode = k.ItemCode,
                   WebAmountOrdered = k.AmtOrdered.ToString(),
                   WebReminderAmount = "",
                   WebAmountWarning="",
                   Type = "Kit"
               };
Craig Stuntz
I got: System.InvalidOperationException: Could not translate expression 'Table(REIP_InventoryKit).Where(k => k.Active).Select(k => new VMPublication() {ID = k.ID, Name = k.Name, WebAmountInStock = k.REIP_XREF_InventoryKits_to_InventoryItems.Select(i => i.REIP_InventoryItem.AmtInStock).Min().ToString(), ItemCode = k.ItemCode, WebAmountOrdered = k.AmtOrdered.ToString(), WebReminderAmount = "", WebAmountWarning = "", Type = "Kit"})' into SQL and could not treat it as a local expression.
Micah
No thats the right answer, now i have to refactor to fix my union issues. Thx!
Micah