0
foreach (var item in MyDatas)
{
List<int> MyIntegerIdValues= item.StringIdValues.Split(',').Select(i => int.Parse(i)).ToList();
foreach (var Id in MyIntegerIdValues)
{
List<string> UserNameList= context.KULLANICILAR.Where(s => s.UserId== Id ).Select(s => s.UserName).ToList();
}
}

I have 3 Id Data Values however , UserNameList always has 1 count data.I can not keep all datas in list.How can i keep my all datas in UserNameList ?

1 Answer 1

2

Let me see if I understand... you have a DB lookup table with user info and you want all the user names from a list of all the user ids? You dont need a foreach loop at all unless you are doing more processing. I suspect even that could be done in the select. Try this linq statement...

List<string> UserNameList = context.KULLANICILAR.Where(x=> MyIntegerIdValues.Contains(x.UserId)).Select(x=>x.UserName).ToList();

As a full LinqPad sample check out the below code. The output is below also...

public class KULLANICILARItem {
    public int UserId {get;set;}
    public string UserName {get;set;}
}

void Main()
{
    // Fake DB Context
    List<KULLANICILARItem> KULLANICILAR = new List<KULLANICILARItem>() {
        new KULLANICILARItem() {
            UserId = 1,
            UserName = "Bob"
        },
        new KULLANICILARItem() {
            UserId = 2,
            UserName = "Jane"
        },
        new KULLANICILARItem() {
            UserId = 3,
            UserName = "Soner"
        },
    };

    // Fake ID List
    List<int> MyIntegerIdValues = new List<int>() {
        1, 2, 3
    };

    List<string> UserNameList = KULLANICILAR.Where(x=> MyIntegerIdValues.Contains(x.UserId)).Select(x=>x.UserName).ToList();

    UserNameList.Dump();
}


Output:
Bob
Jane
Soner

To loop over a list of lists, you need only move the string list definition outside of the foreach loop and then add range.

public class KULLANICILARItem {
    public int UserId {get;set;}
    public string UserName {get;set;}
}

void Main()
{
    // Fake DB Context
    List<KULLANICILARItem> KULLANICILAR = new List<KULLANICILARItem>() {
        new KULLANICILARItem() {
            UserId = 1,
            UserName = "Bob"
        },
        new KULLANICILARItem() {
            UserId = 2,
            UserName = "Jane"
        },
        new KULLANICILARItem() {
            UserId = 3,
            UserName = "Soner"
        },
        new KULLANICILARItem() {
            UserId = 4,
            UserName = "Jim"
        }
    };

    List<string> UserNameList = new List<string>();

    foreach(var StringIdValues in new string[] { "1,2", "3,4"} )
    {
        List<int> MyIntegerIdValues = StringIdValues.Split(',').Select(i => int.Parse(i)).ToList();
        UserNameList.AddRange(KULLANICILAR.Where(x=> MyIntegerIdValues.Contains(x.UserId)).Select(x=>x.UserName).ToList());
    }

    UserNameList.Dump();
}
Sign up to request clarification or add additional context in comments.

3 Comments

it works only for first list i have many lists i need foreach do you have any idea ?
MyIntegerIdValues has datas. 1. data : 1, 2, 3 2. data : 6,7,8 Your code works only for 1. data not second one. That is why i need foreach till datas finish
I think this could be done in far less code, but to stick to your requirements and example I have updated my answer. You need to move the list outside of the foreach loop and add the names to the list using AddRange.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.