Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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...</p> <pre><code>List&lt;string&gt; UserNameList = context.KULLANICILAR.Where(x=&gt; MyIntegerIdValues.Contains(x.UserId)).Select(x=&gt;x.UserName).ToList(); </code></pre> <p>As a full LinqPad sample check out the below code. The output is below also...</p> <pre><code>public class KULLANICILARItem { public int UserId {get;set;} public string UserName {get;set;} } void Main() { // Fake DB Context List&lt;KULLANICILARItem&gt; KULLANICILAR = new List&lt;KULLANICILARItem&gt;() { new KULLANICILARItem() { UserId = 1, UserName = "Bob" }, new KULLANICILARItem() { UserId = 2, UserName = "Jane" }, new KULLANICILARItem() { UserId = 3, UserName = "Soner" }, }; // Fake ID List List&lt;int&gt; MyIntegerIdValues = new List&lt;int&gt;() { 1, 2, 3 }; List&lt;string&gt; UserNameList = KULLANICILAR.Where(x=&gt; MyIntegerIdValues.Contains(x.UserId)).Select(x=&gt;x.UserName).ToList(); UserNameList.Dump(); } Output: Bob Jane Soner </code></pre> <p>To loop over a list of lists, you need only move the string list definition outside of the foreach loop and then add range.</p> <pre><code>public class KULLANICILARItem { public int UserId {get;set;} public string UserName {get;set;} } void Main() { // Fake DB Context List&lt;KULLANICILARItem&gt; KULLANICILAR = new List&lt;KULLANICILARItem&gt;() { 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&lt;string&gt; UserNameList = new List&lt;string&gt;(); foreach(var StringIdValues in new string[] { "1,2", "3,4"} ) { List&lt;int&gt; MyIntegerIdValues = StringIdValues.Split(',').Select(i =&gt; int.Parse(i)).ToList(); UserNameList.AddRange(KULLANICILAR.Where(x=&gt; MyIntegerIdValues.Contains(x.UserId)).Select(x=&gt;x.UserName).ToList()); } UserNameList.Dump(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload