I have a small app that is linked to a SQL table. That particular table has the following fields:
This app is to keep the stock updated.
So for key1=1
, I enter item1
in shortchar05
, number05
(total) I'll have 5
for example. number04
(this is used for how many item1
I have issued) and number03
should be updated stock (difference between number05
and number04
).
I know how to make the app work if values of shortchar05
are distinct (e.g. item1
, item2
, item3
etc.), but that's not what I am interested in.
What if I have the following situation:
key1 shortchar05 number05 number04 number03
===========================================
1 item1 5 3 2
2 item2 7 4 3
3 item3 8 2 6
Now if I am creating the next key1
with value 4 but want to select item1
and now the total would be 2
which would go in number05
and I want to issue 1 piece of item1
.
key1 shortchar05 number05 number04 number03
===========================================
4 item1 2 1 1
For key1=5
I want to select item1
again, so what I would like to have selected is the item1
with the lowest value from column number03
(key=4
and not key1=1
)
key1 shortchar05 number05 number04 number03
===========================================
5 item1 1 0 1
This is what I would like to have, for n number of items, if I select itemX, I'd like to display the duplicate itemX with lowest value in number03
(that would be the updated total)
I hope I have been clear enough
Thank you!
This is simply done when creating a new key1 and entering a new distinct item
foreach (var ttUD04_R in ttUD04)
{
var ttUD04_Recss = (from ttUD04_Row in Db.UD04 where ttUD04_Row.Company == Session.CompanyID orderby ttUD04_Row.Number03 ascending select ttUD04_Row).FirstOrDefault();
if (ttUD04_Recss != null)
{
ttUD04_R.Number05 = ttUD04_Recss.Number03;
ttUD04_R.Number03 = (Convert.ToInt32(ttUD04_R.Number05) - Convert.ToInt32(ttUD04_R.Number04));
}
}
答案 0 :(得分:1)
This query will find ONLY duplicate values and select only the smallest value:
var duplicateMin = (yourTable.GroupBy(x=>x).Where(g=>g.Count()>1).Select(y=>y.DuplicateValue)).Min();
The variable naming in your code looks terrible, it's just about close to impossible to read after you. Please consider renaming your variables. I mean just look at your names: ttUD04_R
, ttUD04
, ttUD04_Recss
, Number05
, Number03
. This makes no sense at all and I can guarantee you that anyone else that will try to read after you will have absolutely no idea what you're doing. And it seems like you don't either.
I'd suggest renaming them to something more sensible like stockItems
, updatedStock
, itemStock
.
答案 1 :(得分:0)
如何在number03中选择具有最低值的itemX(即使重复)?
你如何陈述它并不重要,例如,如果我这样陈述:
从符合条件的所有项目中选择字段的最低值(在本例中为number03)(在本例中为shortchar05 =' item1')
然后对linq的翻译很简单
符合条件的所有项目:
var result = sourceList.Where(x => x.shortchar05 == 'item1');
number03字段的最低值 - 按该字段排序并取第一个字段:
var result = sourceList.Where(x => x.shortchar05 == 'item1')
.OrderByDescending(x => x.number03)
.FirstOrDefault();