EN VI

Get the output from the list c#?

2024-03-12 20:30:07
How to Get the output from the list c#

In a list similar to this: `

public static List<Items> Models()
        {
        List<Items> listOfModels = new List<Items>()
            {
         new Items() {
        ModelID="M10",
        ModelNames = new List<string>() {"model1","model2"}},
         new Items() {
        ModelID="M20",
        ModelName = new List<string>(){"model3","model4","model5"}}
            };
           return listOfModels;
        }

`

How to get the following output? (model1,m10) (model2,m10) (model3,m20) (model4,m20) (model5,m20)

var allModels = Models().SelectMany(c => c.ModelName)

output : model , model2, model3,...

Solution:

This is a small update of your code:

var allModels = Models()
    .SelectMany(item => item.ModelName.Select(model => $"({model},{item.ModelID})"));

then print models :

foreach(var model in allModels)
{
    Console.WriteLine(model);
}
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login