0

There are 2 classes in the program :

ItemUtility class :

public class ItemUtility
{
    public ItemUtility(int item, int utility)
    {
        this.item = item;
        this.utility = utility;
    }

    public int item { get; set; }
    public int utility { get; set; }
}

TransactionTP class :

public class TransactionTP
{
    protected List<ItemUtility> itemsUtilities { get; set; }
    protected int transactionUtility { get; set; }
    public TransactionTP(List<ItemUtility> itemsUtilities, int transactionUtility)
    {
        this.itemsUtilities = itemsUtilities;
        this.transactionUtility = transactionUtility;
    }
}

We have defined a list, ( List<TransactionTP> transactions ) and we assigned the value to this collection

enter image description here

With the command below, I get the first line

    foreach (TransactionTP b in matrix)
{
    matrix[b];
}

How can I get the item and utility of the first line of the first transaction?

According to the photo:

214 + 260 + ...

846 + 552 + ...

I tried the following procedure but it's a mistake :

matrix[a].itemsUtilities[b]
Liam
  • 27,717
  • 28
  • 128
  • 190
Reza Hatami
  • 102
  • 6
  • 5
    _"With the command below, I get the first line"_ No, you should get a compiler error at `matrix[b]`, because `List` has no indexer that takes the object – Tim Schmelter May 11 '18 at 09:51
  • 2
    *How can I get the item and utility of the first line of the first transaction?* `matrix[0].itemsUtilities[0]` would do that. It's really not clear what your asking beyond that – Liam May 11 '18 at 09:53
  • You want to build the sum of both properties? So one `TotalItem` and one `TotalUtility`? – Tim Schmelter May 11 '18 at 09:54
  • You should rework your question and also change the title, _"Check the list of class in main c#"_ doesnt make any sense, titles should also not contain tags. – Tim Schmelter May 11 '18 at 09:56
  • @Tim Schmelter, In fact, I want to check all the transactions, and get the total transactionUtility for each item. for ex : Item 214 is available on the first transaction. So in the other list for field 214, we put the number 2417. Also, if in the second transaction, item 214 is available, add 1744 (transactionUtility for the second transaction) to 2417 and put it at 214 (4161). – Reza Hatami May 11 '18 at 10:18
  • @Amin: still not clear. Edit your question, explain this logic there, provide a meaningful sample(with few items), the desired result and a meaningful title. – Tim Schmelter May 11 '18 at 10:20

3 Answers3

0

matrix is a list of TransactionTP, so when you do foreach you are enumerating list items. Here's what should work:

foreach (TransactionTP b in matrix)
{
    if(b.itemsUtilities.Count > 0)
    {
        // Do something with item / utility...
        b.itemsUtilities[0].item
        b.itemsUtilities[0].utility
    }
}

If you want only the first transaction and first itemsUtilities, then you just need to access it using indexer: matrix[0].itemsUtilities[0].item

Krypt1
  • 1,066
  • 8
  • 14
  • `b.itemsUtilities.Any()` would be more efficient than `Count` – Liam May 11 '18 at 09:57
  • 2
    @Liam `itemsUtilities` is already a `List`, so it stores `Count` internally. What you are saying would be true for extension method `Count()` for `IEnumerable`. – FCin May 11 '18 at 09:58
0

according to the photo:

214 + 260 + ...

846 + 552 + ...

I think you want to iterate the nested lists and sum the item

First things first your going to need to make your properties public (see What is the difference between public, private, protected, and having no access modifier?)

public class TransactionTP
{
    public List<ItemUtility> itemsUtilities { get; set; }
    public  int transactionUtility { get; set; }
    public TransactionTP(List<ItemUtility> itemsUtilities, int transactionUtility)
    {
        this.itemsUtilities = itemsUtilities;
        this.transactionUtility = transactionUtility;
    }
}

Then you can do something like this:

foreach (TransactionTP b in matrix)
{
    int itemCount = 0;
    //remember to null check to be defensive
    if (b.itemsUtilities != null)
    {
        foreach(ItemUtility c in b.itemsUtilities)
        {
            itemCount += c.item;
        }
    }
    //presuming this is an console app?
    Console.Write(itemCount);
}

You could also do this with LINQ:

foreach (TransactionTP b in matrix)
{
    int itemCount = 0;
    //remember to null check to be defensive
    if (b.itemsUtilities != null)
    {
        itemCount = b.itemsUtilities.Sum(s => s.item);
    }
    //presuming this is an console app?
    Console.Write(itemCount);
}
Liam
  • 27,717
  • 28
  • 128
  • 190
  • I received an error , "TransactionTP.itemsUtilities' is inaccessible due to its protection level " – Reza Hatami May 11 '18 at 10:05
  • That's because you set `TransactionTP` properties to be protected, with the currently provided code there's no way to access `TransactionTP` properties. you should change them to public or internal in order to be accessible. – T.Aoukar May 11 '18 at 10:10
  • Why is that `protected`? If you want to access it out of the class your going to need to make it `public`? – Liam May 11 '18 at 10:10
-2

please refer to this tutorial about how foreach works: https://www.dotnetperls.com/foreach

basicly, a foreach loops over a list of items, in your example, b, b is the item and matrix it he list of items.

so if you would change your code to

foreach(var b in matrix)
{
    Console.WriteLine(b.transactionUtility);
}

You can than see what every b.transactionUtility is.

Jean-Paul
  • 380
  • 2
  • 9
  • 26