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.
asp.net linq
add a comment |
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.
asp.net linq
1
What isSilos.LoanSilo
? That still seems to containRelatedTopics
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
add a comment |
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.
asp.net linq
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
asp.net linq
edited Nov 12 at 14:09
asked Nov 11 at 9:55
John Ohara
96211022
96211022
1
What isSilos.LoanSilo
? That still seems to containRelatedTopics
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
add a comment |
1
What isSilos.LoanSilo
? That still seems to containRelatedTopics
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
add a comment |
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)));
add a comment |
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)));
add a comment |
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)));
add a comment |
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)));
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)));
edited Nov 19 at 14:37
answered Nov 19 at 14:25
HeyJude
1,0001228
1,0001228
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
What is
Silos.LoanSilo
? That still seems to containRelatedTopics
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