Project to new class from two linq lists











up vote
0
down vote

favorite












I have an existing class that, paired down, looks like this:



public class SiloNode
{
public string Key { get; set; }
public List<string> RelatedTopics { get; set; }
public string Url { get; set; }
}


Key, is a unique key, while RelatedTopics contains a list of Keys that are related.



I maintain a list of these nodes:



List<SiloNode> MasterList = new List<SiloNode>();


I use a query to extract all the related topics then create some links later:



public static IEnumerable<SiloNode> RelatedNodes(this SiloNode root)
{
return MasterList.Where(x => root.RelatedTopics.Contains(x.Key));
}


All of the above works.



However, I need to change RelatedTopics so that I can add some anchor text that is unique to the relationship.



So, first, I created two more classes:



public class RelatedNode
{
public string AnchorText { get; set; }
public string Key { get; set; }
}

public class NodeLink
{
public NodeLink(string url, string text)
{
Url = url;
Text = text;
}

public string Text { get; set; }
public string Url { get; set; }
}


Then I make changes to my SiloNode class:



public class SiloNode
{
public string Key { get; set; }
public List<RelatedNode> RelatedTopics { get; set; }
public string Url { get; set; }
}


So now, instead of RelatedTopics just containing a simple key, it also contains some anchor text that I would like to apply to that relationship.



Here's the bit where I'm struggling - the code below is incomplete:



public static IEnumerable<NodeLink> RelatedNodes(this SiloNode root)
{
return MasterList.Where(x => root.RelatedTopics.Contains(x.Key))
.Select(y => new NodeLink(y.Url, "HOW DO I GET ANCHOR TEXT?"));
}


I need to link the two sides together in such a way that I can access y.Url and root.RelatedTopics.Text.



I still need to match the related nodes but then project to a new NodeLink. While the key is available in 'x', the anchor text is in root.RelatedTopics. I assume that the current linq structure is insufficient to resolve this query but I'm no expert.



Any help appreciated.










share|improve this question




















  • 1




    What is Silos.LoanSilo? That still seems to contain RelatedTopics as a list of strings, which is a bit confusing.
    – Gert Arnold
    Nov 11 at 14:02










  • Sorry, code copying error.
    – John Ohara
    Nov 12 at 14:09










  • That still seems to contain RelatedTopics as a list of strings - that's where I'm stuck Gert, I don't know what to replace the contains with.
    – John Ohara
    Nov 12 at 14:10










  • @JohnOhara, did my answer help you to solve the issue? If so, please mark it as accepted. If not, let me know and I'll try to help further.
    – HeyJude
    Nov 26 at 14:34















up vote
0
down vote

favorite












I have an existing class that, paired down, looks like this:



public class SiloNode
{
public string Key { get; set; }
public List<string> RelatedTopics { get; set; }
public string Url { get; set; }
}


Key, is a unique key, while RelatedTopics contains a list of Keys that are related.



I maintain a list of these nodes:



List<SiloNode> MasterList = new List<SiloNode>();


I use a query to extract all the related topics then create some links later:



public static IEnumerable<SiloNode> RelatedNodes(this SiloNode root)
{
return MasterList.Where(x => root.RelatedTopics.Contains(x.Key));
}


All of the above works.



However, I need to change RelatedTopics so that I can add some anchor text that is unique to the relationship.



So, first, I created two more classes:



public class RelatedNode
{
public string AnchorText { get; set; }
public string Key { get; set; }
}

public class NodeLink
{
public NodeLink(string url, string text)
{
Url = url;
Text = text;
}

public string Text { get; set; }
public string Url { get; set; }
}


Then I make changes to my SiloNode class:



public class SiloNode
{
public string Key { get; set; }
public List<RelatedNode> RelatedTopics { get; set; }
public string Url { get; set; }
}


So now, instead of RelatedTopics just containing a simple key, it also contains some anchor text that I would like to apply to that relationship.



Here's the bit where I'm struggling - the code below is incomplete:



public static IEnumerable<NodeLink> RelatedNodes(this SiloNode root)
{
return MasterList.Where(x => root.RelatedTopics.Contains(x.Key))
.Select(y => new NodeLink(y.Url, "HOW DO I GET ANCHOR TEXT?"));
}


I need to link the two sides together in such a way that I can access y.Url and root.RelatedTopics.Text.



I still need to match the related nodes but then project to a new NodeLink. While the key is available in 'x', the anchor text is in root.RelatedTopics. I assume that the current linq structure is insufficient to resolve this query but I'm no expert.



Any help appreciated.










share|improve this question




















  • 1




    What is Silos.LoanSilo? That still seems to contain RelatedTopics as a list of strings, which is a bit confusing.
    – Gert Arnold
    Nov 11 at 14:02










  • Sorry, code copying error.
    – John Ohara
    Nov 12 at 14:09










  • That still seems to contain RelatedTopics as a list of strings - that's where I'm stuck Gert, I don't know what to replace the contains with.
    – John Ohara
    Nov 12 at 14:10










  • @JohnOhara, did my answer help you to solve the issue? If so, please mark it as accepted. If not, let me know and I'll try to help further.
    – HeyJude
    Nov 26 at 14:34













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an existing class that, paired down, looks like this:



public class SiloNode
{
public string Key { get; set; }
public List<string> RelatedTopics { get; set; }
public string Url { get; set; }
}


Key, is a unique key, while RelatedTopics contains a list of Keys that are related.



I maintain a list of these nodes:



List<SiloNode> MasterList = new List<SiloNode>();


I use a query to extract all the related topics then create some links later:



public static IEnumerable<SiloNode> RelatedNodes(this SiloNode root)
{
return MasterList.Where(x => root.RelatedTopics.Contains(x.Key));
}


All of the above works.



However, I need to change RelatedTopics so that I can add some anchor text that is unique to the relationship.



So, first, I created two more classes:



public class RelatedNode
{
public string AnchorText { get; set; }
public string Key { get; set; }
}

public class NodeLink
{
public NodeLink(string url, string text)
{
Url = url;
Text = text;
}

public string Text { get; set; }
public string Url { get; set; }
}


Then I make changes to my SiloNode class:



public class SiloNode
{
public string Key { get; set; }
public List<RelatedNode> RelatedTopics { get; set; }
public string Url { get; set; }
}


So now, instead of RelatedTopics just containing a simple key, it also contains some anchor text that I would like to apply to that relationship.



Here's the bit where I'm struggling - the code below is incomplete:



public static IEnumerable<NodeLink> RelatedNodes(this SiloNode root)
{
return MasterList.Where(x => root.RelatedTopics.Contains(x.Key))
.Select(y => new NodeLink(y.Url, "HOW DO I GET ANCHOR TEXT?"));
}


I need to link the two sides together in such a way that I can access y.Url and root.RelatedTopics.Text.



I still need to match the related nodes but then project to a new NodeLink. While the key is available in 'x', the anchor text is in root.RelatedTopics. I assume that the current linq structure is insufficient to resolve this query but I'm no expert.



Any help appreciated.










share|improve this question















I have an existing class that, paired down, looks like this:



public class SiloNode
{
public string Key { get; set; }
public List<string> RelatedTopics { get; set; }
public string Url { get; set; }
}


Key, is a unique key, while RelatedTopics contains a list of Keys that are related.



I maintain a list of these nodes:



List<SiloNode> MasterList = new List<SiloNode>();


I use a query to extract all the related topics then create some links later:



public static IEnumerable<SiloNode> RelatedNodes(this SiloNode root)
{
return MasterList.Where(x => root.RelatedTopics.Contains(x.Key));
}


All of the above works.



However, I need to change RelatedTopics so that I can add some anchor text that is unique to the relationship.



So, first, I created two more classes:



public class RelatedNode
{
public string AnchorText { get; set; }
public string Key { get; set; }
}

public class NodeLink
{
public NodeLink(string url, string text)
{
Url = url;
Text = text;
}

public string Text { get; set; }
public string Url { get; set; }
}


Then I make changes to my SiloNode class:



public class SiloNode
{
public string Key { get; set; }
public List<RelatedNode> RelatedTopics { get; set; }
public string Url { get; set; }
}


So now, instead of RelatedTopics just containing a simple key, it also contains some anchor text that I would like to apply to that relationship.



Here's the bit where I'm struggling - the code below is incomplete:



public static IEnumerable<NodeLink> RelatedNodes(this SiloNode root)
{
return MasterList.Where(x => root.RelatedTopics.Contains(x.Key))
.Select(y => new NodeLink(y.Url, "HOW DO I GET ANCHOR TEXT?"));
}


I need to link the two sides together in such a way that I can access y.Url and root.RelatedTopics.Text.



I still need to match the related nodes but then project to a new NodeLink. While the key is available in 'x', the anchor text is in root.RelatedTopics. I assume that the current linq structure is insufficient to resolve this query but I'm no expert.



Any help appreciated.







asp.net linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 14:09

























asked Nov 11 at 9:55









John Ohara

96211022




96211022








  • 1




    What is Silos.LoanSilo? That still seems to contain RelatedTopics as a list of strings, which is a bit confusing.
    – Gert Arnold
    Nov 11 at 14:02










  • Sorry, code copying error.
    – John Ohara
    Nov 12 at 14:09










  • That still seems to contain RelatedTopics as a list of strings - that's where I'm stuck Gert, I don't know what to replace the contains with.
    – John Ohara
    Nov 12 at 14:10










  • @JohnOhara, did my answer help you to solve the issue? If so, please mark it as accepted. If not, let me know and I'll try to help further.
    – HeyJude
    Nov 26 at 14:34














  • 1




    What is Silos.LoanSilo? That still seems to contain RelatedTopics as a list of strings, which is a bit confusing.
    – Gert Arnold
    Nov 11 at 14:02










  • Sorry, code copying error.
    – John Ohara
    Nov 12 at 14:09










  • That still seems to contain RelatedTopics as a list of strings - that's where I'm stuck Gert, I don't know what to replace the contains with.
    – John Ohara
    Nov 12 at 14:10










  • @JohnOhara, did my answer help you to solve the issue? If so, please mark it as accepted. If not, let me know and I'll try to help further.
    – HeyJude
    Nov 26 at 14:34








1




1




What is Silos.LoanSilo? That still seems to contain RelatedTopics as a list of strings, which is a bit confusing.
– Gert Arnold
Nov 11 at 14:02




What is Silos.LoanSilo? That still seems to contain RelatedTopics as a list of strings, which is a bit confusing.
– Gert Arnold
Nov 11 at 14:02












Sorry, code copying error.
– John Ohara
Nov 12 at 14:09




Sorry, code copying error.
– John Ohara
Nov 12 at 14:09












That still seems to contain RelatedTopics as a list of strings - that's where I'm stuck Gert, I don't know what to replace the contains with.
– John Ohara
Nov 12 at 14:10




That still seems to contain RelatedTopics as a list of strings - that's where I'm stuck Gert, I don't know what to replace the contains with.
– John Ohara
Nov 12 at 14:10












@JohnOhara, did my answer help you to solve the issue? If so, please mark it as accepted. If not, let me know and I'll try to help further.
– HeyJude
Nov 26 at 14:34




@JohnOhara, did my answer help you to solve the issue? If so, please mark it as accepted. If not, let me know and I'll try to help further.
– HeyJude
Nov 26 at 14:34












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Replace your Contains with Any, and add an inner Select:



masterList
.Where(m => m.RelatedTopics.Any(t => m.Key == t.Key))
.Select(m => m.RelatedTopics.Where(m.Key == t.Key)
.Select(t => new NodeLink(m.Url, t.AnchorText)));


Alternatively, do as follows:



masterList
.Select(m => m.RelatedTopics
.Where(t => m.Key == t.Key)
.Select(t => new NodeLink(m.Url, t.AnchorText)));





share|improve this answer























    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: "1"
    };
    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',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53247560%2fproject-to-new-class-from-two-linq-lists%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








    up vote
    0
    down vote













    Replace your Contains with Any, and add an inner Select:



    masterList
    .Where(m => m.RelatedTopics.Any(t => m.Key == t.Key))
    .Select(m => m.RelatedTopics.Where(m.Key == t.Key)
    .Select(t => new NodeLink(m.Url, t.AnchorText)));


    Alternatively, do as follows:



    masterList
    .Select(m => m.RelatedTopics
    .Where(t => m.Key == t.Key)
    .Select(t => new NodeLink(m.Url, t.AnchorText)));





    share|improve this answer



























      up vote
      0
      down vote













      Replace your Contains with Any, and add an inner Select:



      masterList
      .Where(m => m.RelatedTopics.Any(t => m.Key == t.Key))
      .Select(m => m.RelatedTopics.Where(m.Key == t.Key)
      .Select(t => new NodeLink(m.Url, t.AnchorText)));


      Alternatively, do as follows:



      masterList
      .Select(m => m.RelatedTopics
      .Where(t => m.Key == t.Key)
      .Select(t => new NodeLink(m.Url, t.AnchorText)));





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        Replace your Contains with Any, and add an inner Select:



        masterList
        .Where(m => m.RelatedTopics.Any(t => m.Key == t.Key))
        .Select(m => m.RelatedTopics.Where(m.Key == t.Key)
        .Select(t => new NodeLink(m.Url, t.AnchorText)));


        Alternatively, do as follows:



        masterList
        .Select(m => m.RelatedTopics
        .Where(t => m.Key == t.Key)
        .Select(t => new NodeLink(m.Url, t.AnchorText)));





        share|improve this answer














        Replace your Contains with Any, and add an inner Select:



        masterList
        .Where(m => m.RelatedTopics.Any(t => m.Key == t.Key))
        .Select(m => m.RelatedTopics.Where(m.Key == t.Key)
        .Select(t => new NodeLink(m.Url, t.AnchorText)));


        Alternatively, do as follows:



        masterList
        .Select(m => m.RelatedTopics
        .Where(t => m.Key == t.Key)
        .Select(t => new NodeLink(m.Url, t.AnchorText)));






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 at 14:37

























        answered Nov 19 at 14:25









        HeyJude

        1,0001228




        1,0001228






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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.


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





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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.


            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%2fstackoverflow.com%2fquestions%2f53247560%2fproject-to-new-class-from-two-linq-lists%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

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python