Assigning values to array elements based on a look up tableAdjusting integer based on multiple elements in int arrayarray with reusable elementsConcerned with Enums, JSON, and ASP.NET MVCBest way of updating a list of unique itemsAssigning values to a particular table columnImplementation of a string-based loot tableDataDictionary Application - ModelComparison of 2 values based on custom operatorAssigning properties based on string parametersCreating range of numeric values based on array of strings

can anyone help me with this awful query plan?

Function pointer with named arguments?

Mistake in years of experience in resume?

Can an Area of Effect spell cast outside a Prismatic Wall extend inside it?

How to stop co-workers from teasing me because I know Russian?

How can I practically buy stocks?

Pre-plastic human skin alternative

As an international instructor, should I openly talk about my accent?

"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?

How much cash can I safely carry into the USA and avoid civil forfeiture?

How to denote matrix elements succinctly?

Are there physical dangers to preparing a prepared piano?

Why do games have consumables?

"You've called the wrong number" or "You called the wrong number"

Did the BCPL programming language support floats?

How to not starve gigantic beasts

What does the integral of a function times a function of a random variable represent, conceptually?

How could Tony Stark make this in Endgame?

What happens to Mjolnir (Thor's hammer) at the end of Endgame?

What is the most expensive material in the world that could be used to create Pun-Pun's lute?

What happened to Captain America in Endgame?

What is the philosophical significance of speech acts/implicature?

How to limit Drive Letters Windows assigns to new removable USB drives

Extension of 2-adic valuation to the real numbers



Assigning values to array elements based on a look up table


Adjusting integer based on multiple elements in int arrayarray with reusable elementsConcerned with Enums, JSON, and ASP.NET MVCBest way of updating a list of unique itemsAssigning values to a particular table columnImplementation of a string-based loot tableDataDictionary Application - ModelComparison of 2 values based on custom operatorAssigning properties based on string parametersCreating range of numeric values based on array of strings






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2












$begingroup$


HI i am writing a c# program where i need to populate a array based on a look up table and set of string arrays with metadata. My lookup table looks like this (Table with key: transmitter, value: Array of receiver)




LED1: ["px1","px2","px3"],
LED2: ["px4","px5","px6"]



and my meta arrays looks like this (It is dynamic. Just an example. This comes as a response from DB query.)



var transmitters = new string[] "LED1", "LED2" ;
var receivers = new string[] "px1", "px2", "px3", "px4", "px5", "px6" ;


My requirement is



  • If the transmitter LED1 or LED2 (or any other transmitter) is present in the lookup table, the value of the transmitter (ie ["px1","px2","px3"]) has to be compared with the receiver which are present in the lookup and led has to be marked yellow.

    • Orphan tranmitter or/ receiver has to be marked red.


Example



LookUp




LED1: ["px1", "px2", "px3"],
LED2: ["px5", "px8"]



Tranmitters and receivers



var transmitters = new string[] "led1", "led2" ;
var receivers = new string[] "px1", "px2", "px3", "px4", "px5", "px6" ;


the result should be a list as



led1-yellow
px1-yellow
px2-yellow
px3-yellow
led2-yellow
px5-yellow
px4-red
px6-red.


I have written code that works



using System;
using System.Collections.Generic;
using System.Linq;


public class Program

public static void Main()

var transmitters = new string[] "led1", "led2", "led3" ;
var receivers = new string[] "px1", "px2", "px3", "px4", "px5", "px6" ;
var lookup = new Dictionary<string, string[]>()
"led1", new string[] "px1", "px2", "px3" ,
"led2", new string[] "px5", "px8"
;

var blocks = new List<Block>();
var blocksTracker = new List<string>();

foreach (var transmitter in transmitters)

if (lookup.ContainsKey(transmitter))

var receiverLookup = lookup[transmitter];
var intersection = receivers.Intersect(receiverLookup).ToArray();
if (intersection.Length > 0)

blocks.Add(new Block() Id = transmitter, status = "yellow");
blocksTracker.Add(transmitter);
foreach (var receiver in intersection)

blocks.Add(new Block()Id = receiver, status = "yellow");
blocksTracker.Add(receiver);

else

blocks.Add(new Block()Id = transmitter, status = "red");
blocksTracker.Add(transmitter);




var ungrouped = receivers.Except(blocksTracker).ToArray();

foreach (var receiver in ungrouped)

blocks.Add(new Block()Id = receiver, status = "red");
blocksTracker.Add(receiver);


foreach (var i in blocks)

Console.WriteLine(i.Id + "-"+i.status);



public class Block

public string Id get; set;

public string status get; set;




I am new to c# and i wanted to know if there is a better way of doing this. Please help. You can see the working fiddle Here










share|improve this question











$endgroup$











  • $begingroup$
    What's Block? Also, what are the usings?
    $endgroup$
    – Peter Taylor
    11 hours ago











  • $begingroup$
    I have updated the question. Please have a look. Also you can see it working dotnetfiddle.net/d3E7n0
    $endgroup$
    – vikk
    11 hours ago

















2












$begingroup$


HI i am writing a c# program where i need to populate a array based on a look up table and set of string arrays with metadata. My lookup table looks like this (Table with key: transmitter, value: Array of receiver)




LED1: ["px1","px2","px3"],
LED2: ["px4","px5","px6"]



and my meta arrays looks like this (It is dynamic. Just an example. This comes as a response from DB query.)



var transmitters = new string[] "LED1", "LED2" ;
var receivers = new string[] "px1", "px2", "px3", "px4", "px5", "px6" ;


My requirement is



  • If the transmitter LED1 or LED2 (or any other transmitter) is present in the lookup table, the value of the transmitter (ie ["px1","px2","px3"]) has to be compared with the receiver which are present in the lookup and led has to be marked yellow.

    • Orphan tranmitter or/ receiver has to be marked red.


Example



LookUp




LED1: ["px1", "px2", "px3"],
LED2: ["px5", "px8"]



Tranmitters and receivers



var transmitters = new string[] "led1", "led2" ;
var receivers = new string[] "px1", "px2", "px3", "px4", "px5", "px6" ;


the result should be a list as



led1-yellow
px1-yellow
px2-yellow
px3-yellow
led2-yellow
px5-yellow
px4-red
px6-red.


I have written code that works



using System;
using System.Collections.Generic;
using System.Linq;


public class Program

public static void Main()

var transmitters = new string[] "led1", "led2", "led3" ;
var receivers = new string[] "px1", "px2", "px3", "px4", "px5", "px6" ;
var lookup = new Dictionary<string, string[]>()
"led1", new string[] "px1", "px2", "px3" ,
"led2", new string[] "px5", "px8"
;

var blocks = new List<Block>();
var blocksTracker = new List<string>();

foreach (var transmitter in transmitters)

if (lookup.ContainsKey(transmitter))

var receiverLookup = lookup[transmitter];
var intersection = receivers.Intersect(receiverLookup).ToArray();
if (intersection.Length > 0)

blocks.Add(new Block() Id = transmitter, status = "yellow");
blocksTracker.Add(transmitter);
foreach (var receiver in intersection)

blocks.Add(new Block()Id = receiver, status = "yellow");
blocksTracker.Add(receiver);

else

blocks.Add(new Block()Id = transmitter, status = "red");
blocksTracker.Add(transmitter);




var ungrouped = receivers.Except(blocksTracker).ToArray();

foreach (var receiver in ungrouped)

blocks.Add(new Block()Id = receiver, status = "red");
blocksTracker.Add(receiver);


foreach (var i in blocks)

Console.WriteLine(i.Id + "-"+i.status);



public class Block

public string Id get; set;

public string status get; set;




I am new to c# and i wanted to know if there is a better way of doing this. Please help. You can see the working fiddle Here










share|improve this question











$endgroup$











  • $begingroup$
    What's Block? Also, what are the usings?
    $endgroup$
    – Peter Taylor
    11 hours ago











  • $begingroup$
    I have updated the question. Please have a look. Also you can see it working dotnetfiddle.net/d3E7n0
    $endgroup$
    – vikk
    11 hours ago













2












2








2





$begingroup$


HI i am writing a c# program where i need to populate a array based on a look up table and set of string arrays with metadata. My lookup table looks like this (Table with key: transmitter, value: Array of receiver)




LED1: ["px1","px2","px3"],
LED2: ["px4","px5","px6"]



and my meta arrays looks like this (It is dynamic. Just an example. This comes as a response from DB query.)



var transmitters = new string[] "LED1", "LED2" ;
var receivers = new string[] "px1", "px2", "px3", "px4", "px5", "px6" ;


My requirement is



  • If the transmitter LED1 or LED2 (or any other transmitter) is present in the lookup table, the value of the transmitter (ie ["px1","px2","px3"]) has to be compared with the receiver which are present in the lookup and led has to be marked yellow.

    • Orphan tranmitter or/ receiver has to be marked red.


Example



LookUp




LED1: ["px1", "px2", "px3"],
LED2: ["px5", "px8"]



Tranmitters and receivers



var transmitters = new string[] "led1", "led2" ;
var receivers = new string[] "px1", "px2", "px3", "px4", "px5", "px6" ;


the result should be a list as



led1-yellow
px1-yellow
px2-yellow
px3-yellow
led2-yellow
px5-yellow
px4-red
px6-red.


I have written code that works



using System;
using System.Collections.Generic;
using System.Linq;


public class Program

public static void Main()

var transmitters = new string[] "led1", "led2", "led3" ;
var receivers = new string[] "px1", "px2", "px3", "px4", "px5", "px6" ;
var lookup = new Dictionary<string, string[]>()
"led1", new string[] "px1", "px2", "px3" ,
"led2", new string[] "px5", "px8"
;

var blocks = new List<Block>();
var blocksTracker = new List<string>();

foreach (var transmitter in transmitters)

if (lookup.ContainsKey(transmitter))

var receiverLookup = lookup[transmitter];
var intersection = receivers.Intersect(receiverLookup).ToArray();
if (intersection.Length > 0)

blocks.Add(new Block() Id = transmitter, status = "yellow");
blocksTracker.Add(transmitter);
foreach (var receiver in intersection)

blocks.Add(new Block()Id = receiver, status = "yellow");
blocksTracker.Add(receiver);

else

blocks.Add(new Block()Id = transmitter, status = "red");
blocksTracker.Add(transmitter);




var ungrouped = receivers.Except(blocksTracker).ToArray();

foreach (var receiver in ungrouped)

blocks.Add(new Block()Id = receiver, status = "red");
blocksTracker.Add(receiver);


foreach (var i in blocks)

Console.WriteLine(i.Id + "-"+i.status);



public class Block

public string Id get; set;

public string status get; set;




I am new to c# and i wanted to know if there is a better way of doing this. Please help. You can see the working fiddle Here










share|improve this question











$endgroup$




HI i am writing a c# program where i need to populate a array based on a look up table and set of string arrays with metadata. My lookup table looks like this (Table with key: transmitter, value: Array of receiver)




LED1: ["px1","px2","px3"],
LED2: ["px4","px5","px6"]



and my meta arrays looks like this (It is dynamic. Just an example. This comes as a response from DB query.)



var transmitters = new string[] "LED1", "LED2" ;
var receivers = new string[] "px1", "px2", "px3", "px4", "px5", "px6" ;


My requirement is



  • If the transmitter LED1 or LED2 (or any other transmitter) is present in the lookup table, the value of the transmitter (ie ["px1","px2","px3"]) has to be compared with the receiver which are present in the lookup and led has to be marked yellow.

    • Orphan tranmitter or/ receiver has to be marked red.


Example



LookUp




LED1: ["px1", "px2", "px3"],
LED2: ["px5", "px8"]



Tranmitters and receivers



var transmitters = new string[] "led1", "led2" ;
var receivers = new string[] "px1", "px2", "px3", "px4", "px5", "px6" ;


the result should be a list as



led1-yellow
px1-yellow
px2-yellow
px3-yellow
led2-yellow
px5-yellow
px4-red
px6-red.


I have written code that works



using System;
using System.Collections.Generic;
using System.Linq;


public class Program

public static void Main()

var transmitters = new string[] "led1", "led2", "led3" ;
var receivers = new string[] "px1", "px2", "px3", "px4", "px5", "px6" ;
var lookup = new Dictionary<string, string[]>()
"led1", new string[] "px1", "px2", "px3" ,
"led2", new string[] "px5", "px8"
;

var blocks = new List<Block>();
var blocksTracker = new List<string>();

foreach (var transmitter in transmitters)

if (lookup.ContainsKey(transmitter))

var receiverLookup = lookup[transmitter];
var intersection = receivers.Intersect(receiverLookup).ToArray();
if (intersection.Length > 0)

blocks.Add(new Block() Id = transmitter, status = "yellow");
blocksTracker.Add(transmitter);
foreach (var receiver in intersection)

blocks.Add(new Block()Id = receiver, status = "yellow");
blocksTracker.Add(receiver);

else

blocks.Add(new Block()Id = transmitter, status = "red");
blocksTracker.Add(transmitter);




var ungrouped = receivers.Except(blocksTracker).ToArray();

foreach (var receiver in ungrouped)

blocks.Add(new Block()Id = receiver, status = "red");
blocksTracker.Add(receiver);


foreach (var i in blocks)

Console.WriteLine(i.Id + "-"+i.status);



public class Block

public string Id get; set;

public string status get; set;




I am new to c# and i wanted to know if there is a better way of doing this. Please help. You can see the working fiddle Here







c# .net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 11 hours ago









Peter Taylor

18.9k3165




18.9k3165










asked 12 hours ago









vikkvikk

14914




14914











  • $begingroup$
    What's Block? Also, what are the usings?
    $endgroup$
    – Peter Taylor
    11 hours ago











  • $begingroup$
    I have updated the question. Please have a look. Also you can see it working dotnetfiddle.net/d3E7n0
    $endgroup$
    – vikk
    11 hours ago
















  • $begingroup$
    What's Block? Also, what are the usings?
    $endgroup$
    – Peter Taylor
    11 hours ago











  • $begingroup$
    I have updated the question. Please have a look. Also you can see it working dotnetfiddle.net/d3E7n0
    $endgroup$
    – vikk
    11 hours ago















$begingroup$
What's Block? Also, what are the usings?
$endgroup$
– Peter Taylor
11 hours ago





$begingroup$
What's Block? Also, what are the usings?
$endgroup$
– Peter Taylor
11 hours ago













$begingroup$
I have updated the question. Please have a look. Also you can see it working dotnetfiddle.net/d3E7n0
$endgroup$
– vikk
11 hours ago




$begingroup$
I have updated the question. Please have a look. Also you can see it working dotnetfiddle.net/d3E7n0
$endgroup$
– vikk
11 hours ago










1 Answer
1






active

oldest

votes


















5












$begingroup$


 if (lookup.ContainsKey(transmitter))
{
var receiverLookup = lookup[transmitter];



This searches for the KeyValuePair twice. There's a more efficient approach:



 if (lookup.TryGetValue(transmitter, out var receiverLookup))




 var ungrouped = receivers.Except(blocksTracker).ToArray();

foreach (var receiver in ungrouped)

blocks.Add(new Block()Id = receiver, status = "red");
blocksTracker.Add(receiver);




The ToArray() there is unnecessary: the enumerable can be left as a lazy enumerable because the only use is to iterate over it once.





 var intersection = receivers.Intersect(receiverLookup).ToArray();
if (intersection.Length > 0)

blocks.Add(new Block() Id = transmitter, status = "yellow");
blocksTracker.Add(transmitter);
foreach (var receiver in intersection)

blocks.Add(new Block()Id = receiver, status = "yellow");
blocksTracker.Add(receiver);

else

blocks.Add(new Block()Id = transmitter, status = "red");
blocksTracker.Add(transmitter);




This seems rather complicated. I think the whole thing could be simplified:



var transmittersPaired = new HashSet<string>();
var receiversPaired = new HashSet<string>();

foreach (var transmitter in transmitters)

if (lookup.TryGetValue(transmitter, out var receiverLookup) && receiverLookup.Any())

transmittersPaired.Add(transmitter);
foreach (var receiver in receiverLookup)

receiversSeen.Add(receiver);




var blocks = new List<Block>();
foreach (var transmitter in transmitters)

blocks.Add(new Block Id = transmitter, status = transmittersPaired.Contains(transmitter) ? "yellow" : "red" );

foreach (var receiver in receivers)

blocks.Add(new Block Id = receiver, status = receiversPaired.Contains(receiver) ? "yellow" : "red" );



There's still some repeated code, which might be simplified in one of two ways. If there's a guarantee that the transmitters and receivers will never share IDs then transmittersPaired and receiversPaired could be merged into one set, and the foreach loops at the end could be merged into one loop over transmitters.Concat(receivers). Alternatively, a method could be factored out.






share|improve this answer











$endgroup$













    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f219172%2fassigning-values-to-array-elements-based-on-a-look-up-table%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5












    $begingroup$


     if (lookup.ContainsKey(transmitter))
    {
    var receiverLookup = lookup[transmitter];



    This searches for the KeyValuePair twice. There's a more efficient approach:



     if (lookup.TryGetValue(transmitter, out var receiverLookup))




     var ungrouped = receivers.Except(blocksTracker).ToArray();

    foreach (var receiver in ungrouped)

    blocks.Add(new Block()Id = receiver, status = "red");
    blocksTracker.Add(receiver);




    The ToArray() there is unnecessary: the enumerable can be left as a lazy enumerable because the only use is to iterate over it once.





     var intersection = receivers.Intersect(receiverLookup).ToArray();
    if (intersection.Length > 0)

    blocks.Add(new Block() Id = transmitter, status = "yellow");
    blocksTracker.Add(transmitter);
    foreach (var receiver in intersection)

    blocks.Add(new Block()Id = receiver, status = "yellow");
    blocksTracker.Add(receiver);

    else

    blocks.Add(new Block()Id = transmitter, status = "red");
    blocksTracker.Add(transmitter);




    This seems rather complicated. I think the whole thing could be simplified:



    var transmittersPaired = new HashSet<string>();
    var receiversPaired = new HashSet<string>();

    foreach (var transmitter in transmitters)

    if (lookup.TryGetValue(transmitter, out var receiverLookup) && receiverLookup.Any())

    transmittersPaired.Add(transmitter);
    foreach (var receiver in receiverLookup)

    receiversSeen.Add(receiver);




    var blocks = new List<Block>();
    foreach (var transmitter in transmitters)

    blocks.Add(new Block Id = transmitter, status = transmittersPaired.Contains(transmitter) ? "yellow" : "red" );

    foreach (var receiver in receivers)

    blocks.Add(new Block Id = receiver, status = receiversPaired.Contains(receiver) ? "yellow" : "red" );



    There's still some repeated code, which might be simplified in one of two ways. If there's a guarantee that the transmitters and receivers will never share IDs then transmittersPaired and receiversPaired could be merged into one set, and the foreach loops at the end could be merged into one loop over transmitters.Concat(receivers). Alternatively, a method could be factored out.






    share|improve this answer











    $endgroup$

















      5












      $begingroup$


       if (lookup.ContainsKey(transmitter))
      {
      var receiverLookup = lookup[transmitter];



      This searches for the KeyValuePair twice. There's a more efficient approach:



       if (lookup.TryGetValue(transmitter, out var receiverLookup))




       var ungrouped = receivers.Except(blocksTracker).ToArray();

      foreach (var receiver in ungrouped)

      blocks.Add(new Block()Id = receiver, status = "red");
      blocksTracker.Add(receiver);




      The ToArray() there is unnecessary: the enumerable can be left as a lazy enumerable because the only use is to iterate over it once.





       var intersection = receivers.Intersect(receiverLookup).ToArray();
      if (intersection.Length > 0)

      blocks.Add(new Block() Id = transmitter, status = "yellow");
      blocksTracker.Add(transmitter);
      foreach (var receiver in intersection)

      blocks.Add(new Block()Id = receiver, status = "yellow");
      blocksTracker.Add(receiver);

      else

      blocks.Add(new Block()Id = transmitter, status = "red");
      blocksTracker.Add(transmitter);




      This seems rather complicated. I think the whole thing could be simplified:



      var transmittersPaired = new HashSet<string>();
      var receiversPaired = new HashSet<string>();

      foreach (var transmitter in transmitters)

      if (lookup.TryGetValue(transmitter, out var receiverLookup) && receiverLookup.Any())

      transmittersPaired.Add(transmitter);
      foreach (var receiver in receiverLookup)

      receiversSeen.Add(receiver);




      var blocks = new List<Block>();
      foreach (var transmitter in transmitters)

      blocks.Add(new Block Id = transmitter, status = transmittersPaired.Contains(transmitter) ? "yellow" : "red" );

      foreach (var receiver in receivers)

      blocks.Add(new Block Id = receiver, status = receiversPaired.Contains(receiver) ? "yellow" : "red" );



      There's still some repeated code, which might be simplified in one of two ways. If there's a guarantee that the transmitters and receivers will never share IDs then transmittersPaired and receiversPaired could be merged into one set, and the foreach loops at the end could be merged into one loop over transmitters.Concat(receivers). Alternatively, a method could be factored out.






      share|improve this answer











      $endgroup$















        5












        5








        5





        $begingroup$


         if (lookup.ContainsKey(transmitter))
        {
        var receiverLookup = lookup[transmitter];



        This searches for the KeyValuePair twice. There's a more efficient approach:



         if (lookup.TryGetValue(transmitter, out var receiverLookup))




         var ungrouped = receivers.Except(blocksTracker).ToArray();

        foreach (var receiver in ungrouped)

        blocks.Add(new Block()Id = receiver, status = "red");
        blocksTracker.Add(receiver);




        The ToArray() there is unnecessary: the enumerable can be left as a lazy enumerable because the only use is to iterate over it once.





         var intersection = receivers.Intersect(receiverLookup).ToArray();
        if (intersection.Length > 0)

        blocks.Add(new Block() Id = transmitter, status = "yellow");
        blocksTracker.Add(transmitter);
        foreach (var receiver in intersection)

        blocks.Add(new Block()Id = receiver, status = "yellow");
        blocksTracker.Add(receiver);

        else

        blocks.Add(new Block()Id = transmitter, status = "red");
        blocksTracker.Add(transmitter);




        This seems rather complicated. I think the whole thing could be simplified:



        var transmittersPaired = new HashSet<string>();
        var receiversPaired = new HashSet<string>();

        foreach (var transmitter in transmitters)

        if (lookup.TryGetValue(transmitter, out var receiverLookup) && receiverLookup.Any())

        transmittersPaired.Add(transmitter);
        foreach (var receiver in receiverLookup)

        receiversSeen.Add(receiver);




        var blocks = new List<Block>();
        foreach (var transmitter in transmitters)

        blocks.Add(new Block Id = transmitter, status = transmittersPaired.Contains(transmitter) ? "yellow" : "red" );

        foreach (var receiver in receivers)

        blocks.Add(new Block Id = receiver, status = receiversPaired.Contains(receiver) ? "yellow" : "red" );



        There's still some repeated code, which might be simplified in one of two ways. If there's a guarantee that the transmitters and receivers will never share IDs then transmittersPaired and receiversPaired could be merged into one set, and the foreach loops at the end could be merged into one loop over transmitters.Concat(receivers). Alternatively, a method could be factored out.






        share|improve this answer











        $endgroup$




         if (lookup.ContainsKey(transmitter))
        {
        var receiverLookup = lookup[transmitter];



        This searches for the KeyValuePair twice. There's a more efficient approach:



         if (lookup.TryGetValue(transmitter, out var receiverLookup))




         var ungrouped = receivers.Except(blocksTracker).ToArray();

        foreach (var receiver in ungrouped)

        blocks.Add(new Block()Id = receiver, status = "red");
        blocksTracker.Add(receiver);




        The ToArray() there is unnecessary: the enumerable can be left as a lazy enumerable because the only use is to iterate over it once.





         var intersection = receivers.Intersect(receiverLookup).ToArray();
        if (intersection.Length > 0)

        blocks.Add(new Block() Id = transmitter, status = "yellow");
        blocksTracker.Add(transmitter);
        foreach (var receiver in intersection)

        blocks.Add(new Block()Id = receiver, status = "yellow");
        blocksTracker.Add(receiver);

        else

        blocks.Add(new Block()Id = transmitter, status = "red");
        blocksTracker.Add(transmitter);




        This seems rather complicated. I think the whole thing could be simplified:



        var transmittersPaired = new HashSet<string>();
        var receiversPaired = new HashSet<string>();

        foreach (var transmitter in transmitters)

        if (lookup.TryGetValue(transmitter, out var receiverLookup) && receiverLookup.Any())

        transmittersPaired.Add(transmitter);
        foreach (var receiver in receiverLookup)

        receiversSeen.Add(receiver);




        var blocks = new List<Block>();
        foreach (var transmitter in transmitters)

        blocks.Add(new Block Id = transmitter, status = transmittersPaired.Contains(transmitter) ? "yellow" : "red" );

        foreach (var receiver in receivers)

        blocks.Add(new Block Id = receiver, status = receiversPaired.Contains(receiver) ? "yellow" : "red" );



        There's still some repeated code, which might be simplified in one of two ways. If there's a guarantee that the transmitters and receivers will never share IDs then transmittersPaired and receiversPaired could be merged into one set, and the foreach loops at the end could be merged into one loop over transmitters.Concat(receivers). Alternatively, a method could be factored out.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 8 hours ago

























        answered 11 hours ago









        Peter TaylorPeter Taylor

        18.9k3165




        18.9k3165



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f219172%2fassigning-values-to-array-elements-based-on-a-look-up-table%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Nidaros erkebispedøme

            Birsay

            Where did Arya get these scars? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Favourite questions and answers from the 1st quarter of 2019Why did Arya refuse to end it?Has the pronunciation of Arya Stark's name changed?Has Arya forgiven people?Why did Arya Stark lose her vision?Why can Arya still use the faces?Has the Narrow Sea become narrower?Does Arya Stark know how to make poisons outside of the House of Black and White?Why did Nymeria leave Arya?Why did Arya not kill the Lannister soldiers she encountered in the Riverlands?What is the current canonical age of Sansa, Bran and Arya Stark?