简体   繁体   中英

C# Printing out lists | Foreach two lists

I have problem printing out my lists, as I have necessary informations in two separate lists Currently I know how to print out from one list, but can not figure out how to use both lists to print out things

public static void PregledSvihPolicaOsiguranja()
{
    List<PoliceOsiguranja> lPoliceOsiguranja = DohvatiPoliceOsiguranja();
    List<Klijent> lKlijent = DohvatiKlijente();

    var table = new ConsoleTable("OIB", "Broj Police", "Vrsta Osiguranja", "Ime i prezime", "Datum pocetka", "Datum isteka", "Vrijednost");
    foreach (var PoliceOsiguranja in lPoliceOsiguranja)
    {
            table.AddRow(PoliceOsiguranja.OIB, PoliceOsiguranja.BrojPolice, PoliceOsiguranja.VrstaOsiguranja, PoliceOsiguranja.Ime + " " + PoliceOsiguranja.Prezime, PoliceOsiguranja.DatumPocetka, PoliceOsiguranja.DatumIsteka, PoliceOsiguranja.Vrijednost) ;
    }
    table.Write();
}

This part "PoliceOsiguranja.Ime + " " + PoliceOsiguranja.Prezime" should use lists "lKlijent" and print from that JSON file Ime (name) and Prezime (surname)

Thank you!

EDIT: I have tried using Zip but it's not working for me

If you are interested in printing the client with their specific policy, then perhaps do something like this,

    foreach (var client in lKlijent)
    {
        PoliceOsiguranja policy = lPoliceOsiguranja.Where(x => x.OIB == client.OIB).FirstOrDefault();
        table.AddRow(policy.property1, policy.property2, client.property1, client.property2) ;
    }

Depending on how your data look, you might have to run through multiple foreach loops. If you have one policy per client, then above works. If you have one client per policy, you can flip the loop and use Policies as the loop and look up client based on the policy you are iterating through.

If you have multiple clients for each policy, then you are better off using a separate foreach loop as well.

    foreach (var policy in lPoliceOsiguranja)
        foreach (var client in lKlijent.Where(x => x.OID == policy.OID))
            table.AddRow(policy.property1, policy.property2, client.property1, client.property2);

and with same logic, if you have multiple policies per client, flip the loops and iterate over clients first then iterate over policies that match client.OID.

Since I don't have a whole lot to go on from your code I'll give you a more generalized example of using Zip. Lets say for example these are your classes

public class PoliceOsiguranja
{
    public string OIB { get; set; }
}

public class Klijent
{
    public string OIB { get; set; }
}

We will make a new class that represents the combination

public class Combination
{
    public PoliceOsiguranja A { get; set; }
    public Klijent B { get; set; }
}

Using .Zip we will combine your two lists into a list of Combinations. Say we start with these lists

public static List<PoliceOsiguranja> listA = new List<PoliceOsiguranja> 
{
    new PoliceOsiguranja{ OIB = "PoliceOsiguranja1"},
    new PoliceOsiguranja{ OIB = "PoliceOsiguranja2"},
    new PoliceOsiguranja{ OIB = "PoliceOsiguranja3"},
};
public static List<Klijent> listB = new List<Klijent> 
{
    new Klijent { OIB = "klijent1"},
    new Klijent { OIB = "klijent2"},
    new Klijent { OIB = "klijent3"}
};

We can use Zip to make an IEnumerable of combinations

IEnumerable<Combination> combinedList 
     = listA.Zip(listB, (a, b) => new Combination { A = a, B = b });

Now we can use them

foreach(Combination item in combinedList)
{
    Console.WriteLine($"PoliceOsiguranja OIB = {item.A.OIB}, Klijent OIB = {item.B.OIB}");
}

If you needed to sort them, say by OIB, you can do that as well by changing to

IEnumerable<Combination> sortedCombinedList = listA.OrderBy(a => a.OIB).Zip(listB.OrderBy(b => b.OIB), (a, b) => new Combination { A = a, B = b });

but that will depend on your use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM