LINQ call on private List from other class
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Assume I have a class A that has a private List Plist and the functions necessary for basic interaction with it (adding, deleting etc). In my class B, I use these functions, but also want to iterate/search/whatever over it via LINQ in many different ways. Creating a new function for every query seems unreasonable, so I wonder if I could create a function that takes a query as an argument and applies it to the private list. I am aware that it pretty much defeats the point of having the list private in the first place, however Im wondering if it's possible
Technically, I would like it to work like this (which it obviously doesnt, but you get the idea):
public class A
{
private List<C> Plist { get; set; } = new List<C>();
public C Search(string query)
{
this.Plist.query
}
}
public class B
{
A a = new A();
a.Search("Where(i => i.AnotherListInC.Contains(SomeObject))");
}
Is there a way to access a private property with complex LINQ queries, or more generally, concatenate a string to existing code to create new code?
c# linq private
|
show 1 more comment
Assume I have a class A that has a private List Plist and the functions necessary for basic interaction with it (adding, deleting etc). In my class B, I use these functions, but also want to iterate/search/whatever over it via LINQ in many different ways. Creating a new function for every query seems unreasonable, so I wonder if I could create a function that takes a query as an argument and applies it to the private list. I am aware that it pretty much defeats the point of having the list private in the first place, however Im wondering if it's possible
Technically, I would like it to work like this (which it obviously doesnt, but you get the idea):
public class A
{
private List<C> Plist { get; set; } = new List<C>();
public C Search(string query)
{
this.Plist.query
}
}
public class B
{
A a = new A();
a.Search("Where(i => i.AnotherListInC.Contains(SomeObject))");
}
Is there a way to access a private property with complex LINQ queries, or more generally, concatenate a string to existing code to create new code?
c# linq private
1
Could you implementIEnumerable<C>
forA
? Then you can simply callWhere
directly. Or do you want to pass a string?
– Lee
Jan 4 at 10:37
I think Lee's suggestion is good, but let's just assume that you could do what you are asking you would turn compile-time errors into run-time errors which is generally not a great thing.
– Brian Rasmussen
Jan 4 at 10:39
I haven't worked directly with that Interface yet, so I don't know. Im going to look into it, thanks for the hint. However I'm also interested in the stringified-code-as-parameter approach since it's probably very useful for other things.
– Sven Engling
Jan 4 at 10:40
1
May be you should useIReadOnlyCollection
and expose list through it?
– JohnyL
Jan 4 at 10:41
Is passing such queries by string a necessity? If not I'll suggest passing Predicate<T> link as a parameter. Then in Your Search method You gan filter for all elements meeting diven predicate
– Krzysiek Przekwas
Jan 4 at 10:43
|
show 1 more comment
Assume I have a class A that has a private List Plist and the functions necessary for basic interaction with it (adding, deleting etc). In my class B, I use these functions, but also want to iterate/search/whatever over it via LINQ in many different ways. Creating a new function for every query seems unreasonable, so I wonder if I could create a function that takes a query as an argument and applies it to the private list. I am aware that it pretty much defeats the point of having the list private in the first place, however Im wondering if it's possible
Technically, I would like it to work like this (which it obviously doesnt, but you get the idea):
public class A
{
private List<C> Plist { get; set; } = new List<C>();
public C Search(string query)
{
this.Plist.query
}
}
public class B
{
A a = new A();
a.Search("Where(i => i.AnotherListInC.Contains(SomeObject))");
}
Is there a way to access a private property with complex LINQ queries, or more generally, concatenate a string to existing code to create new code?
c# linq private
Assume I have a class A that has a private List Plist and the functions necessary for basic interaction with it (adding, deleting etc). In my class B, I use these functions, but also want to iterate/search/whatever over it via LINQ in many different ways. Creating a new function for every query seems unreasonable, so I wonder if I could create a function that takes a query as an argument and applies it to the private list. I am aware that it pretty much defeats the point of having the list private in the first place, however Im wondering if it's possible
Technically, I would like it to work like this (which it obviously doesnt, but you get the idea):
public class A
{
private List<C> Plist { get; set; } = new List<C>();
public C Search(string query)
{
this.Plist.query
}
}
public class B
{
A a = new A();
a.Search("Where(i => i.AnotherListInC.Contains(SomeObject))");
}
Is there a way to access a private property with complex LINQ queries, or more generally, concatenate a string to existing code to create new code?
c# linq private
c# linq private
asked Jan 4 at 10:35
Sven EnglingSven Engling
1917
1917
1
Could you implementIEnumerable<C>
forA
? Then you can simply callWhere
directly. Or do you want to pass a string?
– Lee
Jan 4 at 10:37
I think Lee's suggestion is good, but let's just assume that you could do what you are asking you would turn compile-time errors into run-time errors which is generally not a great thing.
– Brian Rasmussen
Jan 4 at 10:39
I haven't worked directly with that Interface yet, so I don't know. Im going to look into it, thanks for the hint. However I'm also interested in the stringified-code-as-parameter approach since it's probably very useful for other things.
– Sven Engling
Jan 4 at 10:40
1
May be you should useIReadOnlyCollection
and expose list through it?
– JohnyL
Jan 4 at 10:41
Is passing such queries by string a necessity? If not I'll suggest passing Predicate<T> link as a parameter. Then in Your Search method You gan filter for all elements meeting diven predicate
– Krzysiek Przekwas
Jan 4 at 10:43
|
show 1 more comment
1
Could you implementIEnumerable<C>
forA
? Then you can simply callWhere
directly. Or do you want to pass a string?
– Lee
Jan 4 at 10:37
I think Lee's suggestion is good, but let's just assume that you could do what you are asking you would turn compile-time errors into run-time errors which is generally not a great thing.
– Brian Rasmussen
Jan 4 at 10:39
I haven't worked directly with that Interface yet, so I don't know. Im going to look into it, thanks for the hint. However I'm also interested in the stringified-code-as-parameter approach since it's probably very useful for other things.
– Sven Engling
Jan 4 at 10:40
1
May be you should useIReadOnlyCollection
and expose list through it?
– JohnyL
Jan 4 at 10:41
Is passing such queries by string a necessity? If not I'll suggest passing Predicate<T> link as a parameter. Then in Your Search method You gan filter for all elements meeting diven predicate
– Krzysiek Przekwas
Jan 4 at 10:43
1
1
Could you implement
IEnumerable<C>
for A
? Then you can simply call Where
directly. Or do you want to pass a string?– Lee
Jan 4 at 10:37
Could you implement
IEnumerable<C>
for A
? Then you can simply call Where
directly. Or do you want to pass a string?– Lee
Jan 4 at 10:37
I think Lee's suggestion is good, but let's just assume that you could do what you are asking you would turn compile-time errors into run-time errors which is generally not a great thing.
– Brian Rasmussen
Jan 4 at 10:39
I think Lee's suggestion is good, but let's just assume that you could do what you are asking you would turn compile-time errors into run-time errors which is generally not a great thing.
– Brian Rasmussen
Jan 4 at 10:39
I haven't worked directly with that Interface yet, so I don't know. Im going to look into it, thanks for the hint. However I'm also interested in the stringified-code-as-parameter approach since it's probably very useful for other things.
– Sven Engling
Jan 4 at 10:40
I haven't worked directly with that Interface yet, so I don't know. Im going to look into it, thanks for the hint. However I'm also interested in the stringified-code-as-parameter approach since it's probably very useful for other things.
– Sven Engling
Jan 4 at 10:40
1
1
May be you should use
IReadOnlyCollection
and expose list through it?– JohnyL
Jan 4 at 10:41
May be you should use
IReadOnlyCollection
and expose list through it?– JohnyL
Jan 4 at 10:41
Is passing such queries by string a necessity? If not I'll suggest passing Predicate<T> link as a parameter. Then in Your Search method You gan filter for all elements meeting diven predicate
– Krzysiek Przekwas
Jan 4 at 10:43
Is passing such queries by string a necessity? If not I'll suggest passing Predicate<T> link as a parameter. Then in Your Search method You gan filter for all elements meeting diven predicate
– Krzysiek Przekwas
Jan 4 at 10:43
|
show 1 more comment
1 Answer
1
active
oldest
votes
If you need to use String as parameter it will be hard as you would have to emit code.
If you just want to search private list than create method that accepts predicate as parameter:
public class A
{
private List<C> Plist { get; set; } = new List<C>();
public IEnumerable<C> Search(Func<C, bool> predicate)
{
return this.Plist.Where(predicate);
}
}
public class B
{
public void SomeMethod()
{
A a = new A();
a.Search(i => i.AnotherListInC.Contains(SomeObject));
}
}
Similar method can be written for any Linq
method. Just create method that accepts the same parameters as Linq
method except of first parameter (IEnumerable<TSource>
).
In case you want all Linq
methods available you can implement IEnumerable<C>
on your class A
public class A : IEnumerable<C>
{
private List<C> Plist { get; set; } = new List<C>();
public IEnumerator<C> GetEnumerator()
{
return ((IEnumerable<C>)Plist).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<C>)Plist).GetEnumerator();
}
}
public class B
{
public void SomeMethod()
{
A a = new A();
a.Where(i => i.AnotherListInC.Contains(SomeObject));
a.Any(i => i.AnotherListInC.Contains(SomeObject));
}
}
These looks very well. Func<C,bool> is event better than my Predicate<T> proposed above
– Krzysiek Przekwas
Jan 4 at 10:46
This looks very much like the thing I'm looking for, thank you very much. Another question however is, what if I dont want to use Where, but for example Any. Is there a way to omit the Where from Search and deliver it with SomeMethod, or would I have to create a new function for each?
– Sven Engling
Jan 4 at 10:48
See updated answer
– Pablo notPicasso
Jan 4 at 10:55
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f54037263%2flinq-call-on-private-list-from-other-class%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
If you need to use String as parameter it will be hard as you would have to emit code.
If you just want to search private list than create method that accepts predicate as parameter:
public class A
{
private List<C> Plist { get; set; } = new List<C>();
public IEnumerable<C> Search(Func<C, bool> predicate)
{
return this.Plist.Where(predicate);
}
}
public class B
{
public void SomeMethod()
{
A a = new A();
a.Search(i => i.AnotherListInC.Contains(SomeObject));
}
}
Similar method can be written for any Linq
method. Just create method that accepts the same parameters as Linq
method except of first parameter (IEnumerable<TSource>
).
In case you want all Linq
methods available you can implement IEnumerable<C>
on your class A
public class A : IEnumerable<C>
{
private List<C> Plist { get; set; } = new List<C>();
public IEnumerator<C> GetEnumerator()
{
return ((IEnumerable<C>)Plist).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<C>)Plist).GetEnumerator();
}
}
public class B
{
public void SomeMethod()
{
A a = new A();
a.Where(i => i.AnotherListInC.Contains(SomeObject));
a.Any(i => i.AnotherListInC.Contains(SomeObject));
}
}
These looks very well. Func<C,bool> is event better than my Predicate<T> proposed above
– Krzysiek Przekwas
Jan 4 at 10:46
This looks very much like the thing I'm looking for, thank you very much. Another question however is, what if I dont want to use Where, but for example Any. Is there a way to omit the Where from Search and deliver it with SomeMethod, or would I have to create a new function for each?
– Sven Engling
Jan 4 at 10:48
See updated answer
– Pablo notPicasso
Jan 4 at 10:55
add a comment |
If you need to use String as parameter it will be hard as you would have to emit code.
If you just want to search private list than create method that accepts predicate as parameter:
public class A
{
private List<C> Plist { get; set; } = new List<C>();
public IEnumerable<C> Search(Func<C, bool> predicate)
{
return this.Plist.Where(predicate);
}
}
public class B
{
public void SomeMethod()
{
A a = new A();
a.Search(i => i.AnotherListInC.Contains(SomeObject));
}
}
Similar method can be written for any Linq
method. Just create method that accepts the same parameters as Linq
method except of first parameter (IEnumerable<TSource>
).
In case you want all Linq
methods available you can implement IEnumerable<C>
on your class A
public class A : IEnumerable<C>
{
private List<C> Plist { get; set; } = new List<C>();
public IEnumerator<C> GetEnumerator()
{
return ((IEnumerable<C>)Plist).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<C>)Plist).GetEnumerator();
}
}
public class B
{
public void SomeMethod()
{
A a = new A();
a.Where(i => i.AnotherListInC.Contains(SomeObject));
a.Any(i => i.AnotherListInC.Contains(SomeObject));
}
}
These looks very well. Func<C,bool> is event better than my Predicate<T> proposed above
– Krzysiek Przekwas
Jan 4 at 10:46
This looks very much like the thing I'm looking for, thank you very much. Another question however is, what if I dont want to use Where, but for example Any. Is there a way to omit the Where from Search and deliver it with SomeMethod, or would I have to create a new function for each?
– Sven Engling
Jan 4 at 10:48
See updated answer
– Pablo notPicasso
Jan 4 at 10:55
add a comment |
If you need to use String as parameter it will be hard as you would have to emit code.
If you just want to search private list than create method that accepts predicate as parameter:
public class A
{
private List<C> Plist { get; set; } = new List<C>();
public IEnumerable<C> Search(Func<C, bool> predicate)
{
return this.Plist.Where(predicate);
}
}
public class B
{
public void SomeMethod()
{
A a = new A();
a.Search(i => i.AnotherListInC.Contains(SomeObject));
}
}
Similar method can be written for any Linq
method. Just create method that accepts the same parameters as Linq
method except of first parameter (IEnumerable<TSource>
).
In case you want all Linq
methods available you can implement IEnumerable<C>
on your class A
public class A : IEnumerable<C>
{
private List<C> Plist { get; set; } = new List<C>();
public IEnumerator<C> GetEnumerator()
{
return ((IEnumerable<C>)Plist).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<C>)Plist).GetEnumerator();
}
}
public class B
{
public void SomeMethod()
{
A a = new A();
a.Where(i => i.AnotherListInC.Contains(SomeObject));
a.Any(i => i.AnotherListInC.Contains(SomeObject));
}
}
If you need to use String as parameter it will be hard as you would have to emit code.
If you just want to search private list than create method that accepts predicate as parameter:
public class A
{
private List<C> Plist { get; set; } = new List<C>();
public IEnumerable<C> Search(Func<C, bool> predicate)
{
return this.Plist.Where(predicate);
}
}
public class B
{
public void SomeMethod()
{
A a = new A();
a.Search(i => i.AnotherListInC.Contains(SomeObject));
}
}
Similar method can be written for any Linq
method. Just create method that accepts the same parameters as Linq
method except of first parameter (IEnumerable<TSource>
).
In case you want all Linq
methods available you can implement IEnumerable<C>
on your class A
public class A : IEnumerable<C>
{
private List<C> Plist { get; set; } = new List<C>();
public IEnumerator<C> GetEnumerator()
{
return ((IEnumerable<C>)Plist).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<C>)Plist).GetEnumerator();
}
}
public class B
{
public void SomeMethod()
{
A a = new A();
a.Where(i => i.AnotherListInC.Contains(SomeObject));
a.Any(i => i.AnotherListInC.Contains(SomeObject));
}
}
edited Jan 4 at 10:55
answered Jan 4 at 10:42
Pablo notPicassoPablo notPicasso
2,30331320
2,30331320
These looks very well. Func<C,bool> is event better than my Predicate<T> proposed above
– Krzysiek Przekwas
Jan 4 at 10:46
This looks very much like the thing I'm looking for, thank you very much. Another question however is, what if I dont want to use Where, but for example Any. Is there a way to omit the Where from Search and deliver it with SomeMethod, or would I have to create a new function for each?
– Sven Engling
Jan 4 at 10:48
See updated answer
– Pablo notPicasso
Jan 4 at 10:55
add a comment |
These looks very well. Func<C,bool> is event better than my Predicate<T> proposed above
– Krzysiek Przekwas
Jan 4 at 10:46
This looks very much like the thing I'm looking for, thank you very much. Another question however is, what if I dont want to use Where, but for example Any. Is there a way to omit the Where from Search and deliver it with SomeMethod, or would I have to create a new function for each?
– Sven Engling
Jan 4 at 10:48
See updated answer
– Pablo notPicasso
Jan 4 at 10:55
These looks very well. Func<C,bool> is event better than my Predicate<T> proposed above
– Krzysiek Przekwas
Jan 4 at 10:46
These looks very well. Func<C,bool> is event better than my Predicate<T> proposed above
– Krzysiek Przekwas
Jan 4 at 10:46
This looks very much like the thing I'm looking for, thank you very much. Another question however is, what if I dont want to use Where, but for example Any. Is there a way to omit the Where from Search and deliver it with SomeMethod, or would I have to create a new function for each?
– Sven Engling
Jan 4 at 10:48
This looks very much like the thing I'm looking for, thank you very much. Another question however is, what if I dont want to use Where, but for example Any. Is there a way to omit the Where from Search and deliver it with SomeMethod, or would I have to create a new function for each?
– Sven Engling
Jan 4 at 10:48
See updated answer
– Pablo notPicasso
Jan 4 at 10:55
See updated answer
– Pablo notPicasso
Jan 4 at 10:55
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.
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%2f54037263%2flinq-call-on-private-list-from-other-class%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
Could you implement
IEnumerable<C>
forA
? Then you can simply callWhere
directly. Or do you want to pass a string?– Lee
Jan 4 at 10:37
I think Lee's suggestion is good, but let's just assume that you could do what you are asking you would turn compile-time errors into run-time errors which is generally not a great thing.
– Brian Rasmussen
Jan 4 at 10:39
I haven't worked directly with that Interface yet, so I don't know. Im going to look into it, thanks for the hint. However I'm also interested in the stringified-code-as-parameter approach since it's probably very useful for other things.
– Sven Engling
Jan 4 at 10:40
1
May be you should use
IReadOnlyCollection
and expose list through it?– JohnyL
Jan 4 at 10:41
Is passing such queries by string a necessity? If not I'll suggest passing Predicate<T> link as a parameter. Then in Your Search method You gan filter for all elements meeting diven predicate
– Krzysiek Przekwas
Jan 4 at 10:43