C# class linq extension












1















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.










share|improve this question


















  • 1





    Using interface is the one way.

    – Coder of Code
    Dec 31 '18 at 7:10
















1















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.










share|improve this question


















  • 1





    Using interface is the one way.

    – Coder of Code
    Dec 31 '18 at 7:10














1












1








1








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












2 Answers
2






active

oldest

votes


















1














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();
}





share|improve this answer
























  • This works perfect! Thank you

    – Praneet Nadkar
    Dec 31 '18 at 8:55



















1














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();
}





share|improve this answer


























  • 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













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
});


}
});














draft saved

draft discarded


















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









1














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();
}





share|improve this answer
























  • This works perfect! Thank you

    – Praneet Nadkar
    Dec 31 '18 at 8:55
















1














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();
}





share|improve this answer
























  • This works perfect! Thank you

    – Praneet Nadkar
    Dec 31 '18 at 8:55














1












1








1







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();
}





share|improve this answer













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();
}






share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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













1














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();
}





share|improve this answer


























  • 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


















1














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();
}





share|improve this answer


























  • 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
















1












1








1







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();
}





share|improve this answer















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();
}






share|improve this answer














share|improve this answer



share|improve this answer








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





















  • 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



















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




















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.




draft saved


draft discarded














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





















































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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas