C# class linq extension
I have written an extension method for two classes. Like this:
public static ICollection<Class1> ExceptTestUser(this ICollection<Class1> list)
{
Func<Class1, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
public static ICollection<Class2> ExceptTestUser(this ICollection<Class2> list)
{
Func<Class2, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
Both Class1 and Class2 have Name properties present.
This works fine, however, I am curious if there is any way I can create a generic method for this where I can pass the type T.
I think, this can be done by using System.Linq.Dynamic. Is there any other way to do such a thing.
c# linq
add a comment |
I have written an extension method for two classes. Like this:
public static ICollection<Class1> ExceptTestUser(this ICollection<Class1> list)
{
Func<Class1, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
public static ICollection<Class2> ExceptTestUser(this ICollection<Class2> list)
{
Func<Class2, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
Both Class1 and Class2 have Name properties present.
This works fine, however, I am curious if there is any way I can create a generic method for this where I can pass the type T.
I think, this can be done by using System.Linq.Dynamic. Is there any other way to do such a thing.
c# linq
1
Using interface is the one way.
– Coder of Code
Dec 31 '18 at 7:10
add a comment |
I have written an extension method for two classes. Like this:
public static ICollection<Class1> ExceptTestUser(this ICollection<Class1> list)
{
Func<Class1, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
public static ICollection<Class2> ExceptTestUser(this ICollection<Class2> list)
{
Func<Class2, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
Both Class1 and Class2 have Name properties present.
This works fine, however, I am curious if there is any way I can create a generic method for this where I can pass the type T.
I think, this can be done by using System.Linq.Dynamic. Is there any other way to do such a thing.
c# linq
I have written an extension method for two classes. Like this:
public static ICollection<Class1> ExceptTestUser(this ICollection<Class1> list)
{
Func<Class1, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
public static ICollection<Class2> ExceptTestUser(this ICollection<Class2> list)
{
Func<Class2, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
Both Class1 and Class2 have Name properties present.
This works fine, however, I am curious if there is any way I can create a generic method for this where I can pass the type T.
I think, this can be done by using System.Linq.Dynamic. Is there any other way to do such a thing.
c# linq
c# linq
asked Dec 31 '18 at 6:59
Praneet NadkarPraneet Nadkar
495212
495212
1
Using interface is the one way.
– Coder of Code
Dec 31 '18 at 7:10
add a comment |
1
Using interface is the one way.
– Coder of Code
Dec 31 '18 at 7:10
1
1
Using interface is the one way.
– Coder of Code
Dec 31 '18 at 7:10
Using interface is the one way.
– Coder of Code
Dec 31 '18 at 7:10
add a comment |
2 Answers
2
active
oldest
votes
You can create an interface called IHasName
and make Class1
and Class2
implement it:
interface IHasName {
string Name { get; }
}
class Class1 : IHasName {
...
}
class Class2 : IHasName {
...
}
Then you can write a single method like this:
public static ICollection<T> ExceptTestUser<T>(this ICollection<T> list) where T : IHasName
{
Func<T, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
This works perfect! Thank you
– Praneet Nadkar
Dec 31 '18 at 8:55
add a comment |
If you want to use the same extension method for classes without interface, you need to use reflection to get the property.
// Uses reflection to return value of the property or null
public static T GetPropValue<T>(this object src, string propName) where T : class
{
return src.GetType().GetProperty(propName)?.GetValue(src, null) as T;
}
public static ICollection<T> ExceptTestUser<T>(this ICollection<T> list)
{
// If property exists, do equality check, otherwise just accept the value
Func<T, bool> _func = i => !i.GetPropValue<string>("Name")?.Equals("Test", StringComparison.InvariantCultureIgnoreCase) ?? true;
return list.Where(_func).ToList();
}
What if the class does not have the property "Name", it would throw an exception?
– Praneet Nadkar
Dec 31 '18 at 8:57
It does not throw an exception, because inGetPropValue<T>
the GetProperty method returns null and the next operator is null-coalescing?.
in case of missing property. Also theExceptTestUser
has this null-coalescing operation after theGetPropValue
and it defaults to true in case of nulls. This worked on my test cases without exceptions.
– Wote
Dec 31 '18 at 12:14
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%2f53984552%2fc-sharp-class-linq-extension%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can create an interface called IHasName
and make Class1
and Class2
implement it:
interface IHasName {
string Name { get; }
}
class Class1 : IHasName {
...
}
class Class2 : IHasName {
...
}
Then you can write a single method like this:
public static ICollection<T> ExceptTestUser<T>(this ICollection<T> list) where T : IHasName
{
Func<T, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
This works perfect! Thank you
– Praneet Nadkar
Dec 31 '18 at 8:55
add a comment |
You can create an interface called IHasName
and make Class1
and Class2
implement it:
interface IHasName {
string Name { get; }
}
class Class1 : IHasName {
...
}
class Class2 : IHasName {
...
}
Then you can write a single method like this:
public static ICollection<T> ExceptTestUser<T>(this ICollection<T> list) where T : IHasName
{
Func<T, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
This works perfect! Thank you
– Praneet Nadkar
Dec 31 '18 at 8:55
add a comment |
You can create an interface called IHasName
and make Class1
and Class2
implement it:
interface IHasName {
string Name { get; }
}
class Class1 : IHasName {
...
}
class Class2 : IHasName {
...
}
Then you can write a single method like this:
public static ICollection<T> ExceptTestUser<T>(this ICollection<T> list) where T : IHasName
{
Func<T, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
You can create an interface called IHasName
and make Class1
and Class2
implement it:
interface IHasName {
string Name { get; }
}
class Class1 : IHasName {
...
}
class Class2 : IHasName {
...
}
Then you can write a single method like this:
public static ICollection<T> ExceptTestUser<T>(this ICollection<T> list) where T : IHasName
{
Func<T, bool> _func = i => !i.Name.Equals("Test", StringComparison.InvariantCultureIgnoreCase);
return list.Where(_func).ToList();
}
answered Dec 31 '18 at 7:07
SweeperSweeper
66.8k1073139
66.8k1073139
This works perfect! Thank you
– Praneet Nadkar
Dec 31 '18 at 8:55
add a comment |
This works perfect! Thank you
– Praneet Nadkar
Dec 31 '18 at 8:55
This works perfect! Thank you
– Praneet Nadkar
Dec 31 '18 at 8:55
This works perfect! Thank you
– Praneet Nadkar
Dec 31 '18 at 8:55
add a comment |
If you want to use the same extension method for classes without interface, you need to use reflection to get the property.
// Uses reflection to return value of the property or null
public static T GetPropValue<T>(this object src, string propName) where T : class
{
return src.GetType().GetProperty(propName)?.GetValue(src, null) as T;
}
public static ICollection<T> ExceptTestUser<T>(this ICollection<T> list)
{
// If property exists, do equality check, otherwise just accept the value
Func<T, bool> _func = i => !i.GetPropValue<string>("Name")?.Equals("Test", StringComparison.InvariantCultureIgnoreCase) ?? true;
return list.Where(_func).ToList();
}
What if the class does not have the property "Name", it would throw an exception?
– Praneet Nadkar
Dec 31 '18 at 8:57
It does not throw an exception, because inGetPropValue<T>
the GetProperty method returns null and the next operator is null-coalescing?.
in case of missing property. Also theExceptTestUser
has this null-coalescing operation after theGetPropValue
and it defaults to true in case of nulls. This worked on my test cases without exceptions.
– Wote
Dec 31 '18 at 12:14
add a comment |
If you want to use the same extension method for classes without interface, you need to use reflection to get the property.
// Uses reflection to return value of the property or null
public static T GetPropValue<T>(this object src, string propName) where T : class
{
return src.GetType().GetProperty(propName)?.GetValue(src, null) as T;
}
public static ICollection<T> ExceptTestUser<T>(this ICollection<T> list)
{
// If property exists, do equality check, otherwise just accept the value
Func<T, bool> _func = i => !i.GetPropValue<string>("Name")?.Equals("Test", StringComparison.InvariantCultureIgnoreCase) ?? true;
return list.Where(_func).ToList();
}
What if the class does not have the property "Name", it would throw an exception?
– Praneet Nadkar
Dec 31 '18 at 8:57
It does not throw an exception, because inGetPropValue<T>
the GetProperty method returns null and the next operator is null-coalescing?.
in case of missing property. Also theExceptTestUser
has this null-coalescing operation after theGetPropValue
and it defaults to true in case of nulls. This worked on my test cases without exceptions.
– Wote
Dec 31 '18 at 12:14
add a comment |
If you want to use the same extension method for classes without interface, you need to use reflection to get the property.
// Uses reflection to return value of the property or null
public static T GetPropValue<T>(this object src, string propName) where T : class
{
return src.GetType().GetProperty(propName)?.GetValue(src, null) as T;
}
public static ICollection<T> ExceptTestUser<T>(this ICollection<T> list)
{
// If property exists, do equality check, otherwise just accept the value
Func<T, bool> _func = i => !i.GetPropValue<string>("Name")?.Equals("Test", StringComparison.InvariantCultureIgnoreCase) ?? true;
return list.Where(_func).ToList();
}
If you want to use the same extension method for classes without interface, you need to use reflection to get the property.
// Uses reflection to return value of the property or null
public static T GetPropValue<T>(this object src, string propName) where T : class
{
return src.GetType().GetProperty(propName)?.GetValue(src, null) as T;
}
public static ICollection<T> ExceptTestUser<T>(this ICollection<T> list)
{
// If property exists, do equality check, otherwise just accept the value
Func<T, bool> _func = i => !i.GetPropValue<string>("Name")?.Equals("Test", StringComparison.InvariantCultureIgnoreCase) ?? true;
return list.Where(_func).ToList();
}
edited Dec 31 '18 at 7:46
answered Dec 31 '18 at 7:37
WoteWote
986
986
What if the class does not have the property "Name", it would throw an exception?
– Praneet Nadkar
Dec 31 '18 at 8:57
It does not throw an exception, because inGetPropValue<T>
the GetProperty method returns null and the next operator is null-coalescing?.
in case of missing property. Also theExceptTestUser
has this null-coalescing operation after theGetPropValue
and it defaults to true in case of nulls. This worked on my test cases without exceptions.
– Wote
Dec 31 '18 at 12:14
add a comment |
What if the class does not have the property "Name", it would throw an exception?
– Praneet Nadkar
Dec 31 '18 at 8:57
It does not throw an exception, because inGetPropValue<T>
the GetProperty method returns null and the next operator is null-coalescing?.
in case of missing property. Also theExceptTestUser
has this null-coalescing operation after theGetPropValue
and it defaults to true in case of nulls. This worked on my test cases without exceptions.
– Wote
Dec 31 '18 at 12:14
What if the class does not have the property "Name", it would throw an exception?
– Praneet Nadkar
Dec 31 '18 at 8:57
What if the class does not have the property "Name", it would throw an exception?
– Praneet Nadkar
Dec 31 '18 at 8:57
It does not throw an exception, because in
GetPropValue<T>
the GetProperty method returns null and the next operator is null-coalescing ?.
in case of missing property. Also the ExceptTestUser
has this null-coalescing operation after the GetPropValue
and it defaults to true in case of nulls. This worked on my test cases without exceptions.– Wote
Dec 31 '18 at 12:14
It does not throw an exception, because in
GetPropValue<T>
the GetProperty method returns null and the next operator is null-coalescing ?.
in case of missing property. Also the ExceptTestUser
has this null-coalescing operation after the GetPropValue
and it defaults to true in case of nulls. This worked on my test cases without exceptions.– Wote
Dec 31 '18 at 12:14
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%2f53984552%2fc-sharp-class-linq-extension%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
Using interface is the one way.
– Coder of Code
Dec 31 '18 at 7:10