An elegant way to check if any items which has the same childrens exist
I have this item:
public partial class PACK
{
public int PACK_IDE { get; set; }
public string PACK_DESCR { get; set; }
public Nullable<System.DateTime> PACK_DATE_CREATED { get; set; }
public Nullable<System.DateTime> PACK_DATE_MODIFIED { get; set; }
public Nullable<System.DateTime> PACK_DATE_LAST_CALC { get; set; }
public decimal PACK_COST { get; set; }
public int PACK_QTY_POSS { get; set; }
public string PACK_NOTE { get; set; }
public int PACK_QTY_SOLD { get; set; }
public decimal PACK_AVRG_SELL_PRICE { get; set; }
public Nullable<int> PACK_DESTINATION { get; set; }
public virtual ICollection<INVENTORY_PACK> INVENTORY_PACK { get; set; }
}
Which contains, as you can see, a list of Inventory Packs which are shaped
like this:
public partial class INVENTORY_PACK
{
public int INVENT_PACK_IDE { get; set; }
public int INVENT_IDE { get; set; }
public int PACK_IDE { get; set; }
public int QTY { get; set; }
public virtual INVENTORY INVENTORY { get; set; }
public virtual PACK PACK { get; set; }
}
And, lastly, the Inventory Items, which has 2 important fields that are of
importance right now:
public partial class INVENTORY
{
public int INVENT_IDE { get; set; }
public Nullable<int> CARD_IDE { get; set; }
public Nullable<int> INVENT_NB_IN_STOCK { get; set; }
public Nullable<int> INVENT_NB_QT_SOLD { get; set; }
public string INVENT_ITEM_STATE { get; set; }
public virtual CARD CARD { get; set; }
public virtual ICollection<INVENTORY_PACK> INVENTORY_PACK { get; set; }
}
When I actually save or create a new pack, I need to find a way to check
if the actual pack exists that has the exact same Inventory Items based on
INVENT_ITEM_STATE and CARD_IDE and, also, of QTY in INVENTORY_PACK. If
these three values are identical, then we may consider having the same
children. I basically need to search through any Packs (using Linq or any
Linq-To-Sql call) which childrens are the same as the one I have right
now, but I don't really know how to do this except for massive
mind-blowing for/foreach loops.
EDIT
As requested, here's an example of what I've been trying to do.
internal void CreatePack(PACK _pack)
{
using (TransactionScope scope = new TransactionScope())
{
try
{
var packQry = from pa in mDb.PACK
select pa;
if (!packQry.Any())
{
mDb.PACK.Add(_pack);
mDb.SaveChanges();
int packID = mDb.PACK.Max(_x => _x.PACK_IDE);
foreach (INVENTORY_PACK inventoryPack in
_pack.INVENTORY_PACK)
{
inventoryPack.PACK_IDE = packID;
mDb.SaveChanges();
}
}
else
{
List<PACK> listPacks = packQry.ToList();
foreach (PACK listPack in listPacks)
{
foreach (INVENTORY_PACK pack in _pack.INVENTORY_PACK)
{
// And that's where I get massively confused.
}
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
scope.Complete();
}
}
EXAMPLE
I think that I need to clarify my need. Here's an example.
Say that I have 1 PACK containing 2 INVENTORY_PACK: 1 is an INVENTORY item
with INVENT_IDE 1234, CARD_IDE 4321, QTY is 1, and INVENT_ITEM_STATE is
PERFECT. The second object is INVENT_IDE 4567, CARD_IDE 7654, QTY is 2 and
INVENT_ITEM_STATE PERFECT.
I need to check through the packages to see if there's already a package
containing exactly there two items in the selected parameters. So there
are many possibilities:
If we have another existing PACK that has the same items, quantities and
IDS, we have a perfect match and we consider that the PACK already exists;
If there is a PACK containing the same items, but with another one (3
items in fact), is it considered another pack; then we do not have a
match;
If any package has only one of these items, we do not have a match.
No comments:
Post a Comment