Passing generic class to amethod that returns a generic ObservableCollection of the same class that is passed











up vote
0
down vote

favorite












I am working with pulling data from a DB in WPF and doing it with multiple different tables, which I have modeled as POCO's in C#. I want to be able to create a method that is flexible enough to handle any of these POCO's being passed in, and then return an observable collection of the same class to the caller.



I have something set up that I haven't tested yet, but I know this probably isn't the best way to implement this so I wanted to get some advice on how to best do this before I even bothered troubleshooting or attempting to get this to work:



 public static ObservableCollection<object> SQLAuthentication(ObservableCollection<object> myCollection, object myClass, String sql)
{
var conn = new SqlConnection();

var paramList = GenerateSQLParameters(myClass, null);
var tempModel = Global.GenerateNewInstance(myClass);

//get the type
Type model = tempModel.GetType();
var prop = model.GetProperties();
PropertyInfo pi;

using (getConnection(conn))
{
conn.Open();
SqlCommand cmd;
SqlDataReader reader;

cmd = new SqlCommand(sql, conn);
reader = cmd.ExecuteReader();

while (reader.Read())
{

//set the values for each property in the model
foreach (var p in prop)
{
pi = tempModel.GetType().GetProperty(p.Name);
pi.SetValue(tempModel, reader[p.Name]);
}
myCollection.Add(tempModel);
}
reader.Close();
cmd.Dispose();
}

return myCollection;
}









share|improve this question






















  • That sounds like a XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
    – Christopher
    Nov 10 at 21:46










  • Any reason why you're not just using Entity framework instead, or any other ORM
    – Aydin Adn
    Nov 10 at 21:56








  • 1




    You could declare it as public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class .
    – John B. Lambe
    Nov 10 at 21:59










  • @Aydin Adn yes I would love to but unfortunately its a corporate project and we have no access to nuget.
    – MattE
    Nov 10 at 23:14










  • @Christopher I'm not sure what you mean. I want to be able to query any table in the DB generically and return the values for it. Is there something else I should be asking? Not trying to be a smart ass, just trying to understand what you are getting at
    – MattE
    Nov 11 at 0:00















up vote
0
down vote

favorite












I am working with pulling data from a DB in WPF and doing it with multiple different tables, which I have modeled as POCO's in C#. I want to be able to create a method that is flexible enough to handle any of these POCO's being passed in, and then return an observable collection of the same class to the caller.



I have something set up that I haven't tested yet, but I know this probably isn't the best way to implement this so I wanted to get some advice on how to best do this before I even bothered troubleshooting or attempting to get this to work:



 public static ObservableCollection<object> SQLAuthentication(ObservableCollection<object> myCollection, object myClass, String sql)
{
var conn = new SqlConnection();

var paramList = GenerateSQLParameters(myClass, null);
var tempModel = Global.GenerateNewInstance(myClass);

//get the type
Type model = tempModel.GetType();
var prop = model.GetProperties();
PropertyInfo pi;

using (getConnection(conn))
{
conn.Open();
SqlCommand cmd;
SqlDataReader reader;

cmd = new SqlCommand(sql, conn);
reader = cmd.ExecuteReader();

while (reader.Read())
{

//set the values for each property in the model
foreach (var p in prop)
{
pi = tempModel.GetType().GetProperty(p.Name);
pi.SetValue(tempModel, reader[p.Name]);
}
myCollection.Add(tempModel);
}
reader.Close();
cmd.Dispose();
}

return myCollection;
}









share|improve this question






















  • That sounds like a XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
    – Christopher
    Nov 10 at 21:46










  • Any reason why you're not just using Entity framework instead, or any other ORM
    – Aydin Adn
    Nov 10 at 21:56








  • 1




    You could declare it as public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class .
    – John B. Lambe
    Nov 10 at 21:59










  • @Aydin Adn yes I would love to but unfortunately its a corporate project and we have no access to nuget.
    – MattE
    Nov 10 at 23:14










  • @Christopher I'm not sure what you mean. I want to be able to query any table in the DB generically and return the values for it. Is there something else I should be asking? Not trying to be a smart ass, just trying to understand what you are getting at
    – MattE
    Nov 11 at 0:00













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am working with pulling data from a DB in WPF and doing it with multiple different tables, which I have modeled as POCO's in C#. I want to be able to create a method that is flexible enough to handle any of these POCO's being passed in, and then return an observable collection of the same class to the caller.



I have something set up that I haven't tested yet, but I know this probably isn't the best way to implement this so I wanted to get some advice on how to best do this before I even bothered troubleshooting or attempting to get this to work:



 public static ObservableCollection<object> SQLAuthentication(ObservableCollection<object> myCollection, object myClass, String sql)
{
var conn = new SqlConnection();

var paramList = GenerateSQLParameters(myClass, null);
var tempModel = Global.GenerateNewInstance(myClass);

//get the type
Type model = tempModel.GetType();
var prop = model.GetProperties();
PropertyInfo pi;

using (getConnection(conn))
{
conn.Open();
SqlCommand cmd;
SqlDataReader reader;

cmd = new SqlCommand(sql, conn);
reader = cmd.ExecuteReader();

while (reader.Read())
{

//set the values for each property in the model
foreach (var p in prop)
{
pi = tempModel.GetType().GetProperty(p.Name);
pi.SetValue(tempModel, reader[p.Name]);
}
myCollection.Add(tempModel);
}
reader.Close();
cmd.Dispose();
}

return myCollection;
}









share|improve this question













I am working with pulling data from a DB in WPF and doing it with multiple different tables, which I have modeled as POCO's in C#. I want to be able to create a method that is flexible enough to handle any of these POCO's being passed in, and then return an observable collection of the same class to the caller.



I have something set up that I haven't tested yet, but I know this probably isn't the best way to implement this so I wanted to get some advice on how to best do this before I even bothered troubleshooting or attempting to get this to work:



 public static ObservableCollection<object> SQLAuthentication(ObservableCollection<object> myCollection, object myClass, String sql)
{
var conn = new SqlConnection();

var paramList = GenerateSQLParameters(myClass, null);
var tempModel = Global.GenerateNewInstance(myClass);

//get the type
Type model = tempModel.GetType();
var prop = model.GetProperties();
PropertyInfo pi;

using (getConnection(conn))
{
conn.Open();
SqlCommand cmd;
SqlDataReader reader;

cmd = new SqlCommand(sql, conn);
reader = cmd.ExecuteReader();

while (reader.Read())
{

//set the values for each property in the model
foreach (var p in prop)
{
pi = tempModel.GetType().GetProperty(p.Name);
pi.SetValue(tempModel, reader[p.Name]);
}
myCollection.Add(tempModel);
}
reader.Close();
cmd.Dispose();
}

return myCollection;
}






c# wpf generics observablecollection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 21:34









MattE

390216




390216












  • That sounds like a XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
    – Christopher
    Nov 10 at 21:46










  • Any reason why you're not just using Entity framework instead, or any other ORM
    – Aydin Adn
    Nov 10 at 21:56








  • 1




    You could declare it as public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class .
    – John B. Lambe
    Nov 10 at 21:59










  • @Aydin Adn yes I would love to but unfortunately its a corporate project and we have no access to nuget.
    – MattE
    Nov 10 at 23:14










  • @Christopher I'm not sure what you mean. I want to be able to query any table in the DB generically and return the values for it. Is there something else I should be asking? Not trying to be a smart ass, just trying to understand what you are getting at
    – MattE
    Nov 11 at 0:00


















  • That sounds like a XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
    – Christopher
    Nov 10 at 21:46










  • Any reason why you're not just using Entity framework instead, or any other ORM
    – Aydin Adn
    Nov 10 at 21:56








  • 1




    You could declare it as public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class .
    – John B. Lambe
    Nov 10 at 21:59










  • @Aydin Adn yes I would love to but unfortunately its a corporate project and we have no access to nuget.
    – MattE
    Nov 10 at 23:14










  • @Christopher I'm not sure what you mean. I want to be able to query any table in the DB generically and return the values for it. Is there something else I should be asking? Not trying to be a smart ass, just trying to understand what you are getting at
    – MattE
    Nov 11 at 0:00
















That sounds like a XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
– Christopher
Nov 10 at 21:46




That sounds like a XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
– Christopher
Nov 10 at 21:46












Any reason why you're not just using Entity framework instead, or any other ORM
– Aydin Adn
Nov 10 at 21:56






Any reason why you're not just using Entity framework instead, or any other ORM
– Aydin Adn
Nov 10 at 21:56






1




1




You could declare it as public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class .
– John B. Lambe
Nov 10 at 21:59




You could declare it as public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class .
– John B. Lambe
Nov 10 at 21:59












@Aydin Adn yes I would love to but unfortunately its a corporate project and we have no access to nuget.
– MattE
Nov 10 at 23:14




@Aydin Adn yes I would love to but unfortunately its a corporate project and we have no access to nuget.
– MattE
Nov 10 at 23:14












@Christopher I'm not sure what you mean. I want to be able to query any table in the DB generically and return the values for it. Is there something else I should be asking? Not trying to be a smart ass, just trying to understand what you are getting at
– MattE
Nov 11 at 0:00




@Christopher I'm not sure what you mean. I want to be able to query any table in the DB generically and return the values for it. Is there something else I should be asking? Not trying to be a smart ass, just trying to understand what you are getting at
– MattE
Nov 11 at 0:00












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You can use generic types in the method signature its self, you dont need to use object collection... and as im writing this John B hit the nail on its head



public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class


@John, if you post it as an answer ill remove this since you did technically shout it out first, just leave a comment if you do so i get the notification






share|improve this answer





















  • Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
    – MattE
    Nov 10 at 23:22










  • Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
    – MattE
    Nov 10 at 23:55













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%2f53243633%2fpassing-generic-class-to-amethod-that-returns-a-generic-observablecollection-of%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
1
down vote



accepted










You can use generic types in the method signature its self, you dont need to use object collection... and as im writing this John B hit the nail on its head



public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class


@John, if you post it as an answer ill remove this since you did technically shout it out first, just leave a comment if you do so i get the notification






share|improve this answer





















  • Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
    – MattE
    Nov 10 at 23:22










  • Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
    – MattE
    Nov 10 at 23:55

















up vote
1
down vote



accepted










You can use generic types in the method signature its self, you dont need to use object collection... and as im writing this John B hit the nail on its head



public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class


@John, if you post it as an answer ill remove this since you did technically shout it out first, just leave a comment if you do so i get the notification






share|improve this answer





















  • Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
    – MattE
    Nov 10 at 23:22










  • Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
    – MattE
    Nov 10 at 23:55















up vote
1
down vote



accepted







up vote
1
down vote



accepted






You can use generic types in the method signature its self, you dont need to use object collection... and as im writing this John B hit the nail on its head



public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class


@John, if you post it as an answer ill remove this since you did technically shout it out first, just leave a comment if you do so i get the notification






share|improve this answer












You can use generic types in the method signature its self, you dont need to use object collection... and as im writing this John B hit the nail on its head



public static ObservableCollection<T> SQLAuthentication(ObservableCollection<T> myCollection, T myClass, String sql) where T: class


@John, if you post it as an answer ill remove this since you did technically shout it out first, just leave a comment if you do so i get the notification







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 21:59









Aydin Adn

9,45422036




9,45422036












  • Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
    – MattE
    Nov 10 at 23:22










  • Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
    – MattE
    Nov 10 at 23:55




















  • Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
    – MattE
    Nov 10 at 23:22










  • Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
    – MattE
    Nov 10 at 23:55


















Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
– MattE
Nov 10 at 23:22




Thanks! I had tried but forgot to add the T for myClass. Just getting back into the awing of things in C#...I've been in mostly javascript/angular the past year and a half...
– MattE
Nov 10 at 23:22












Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
– MattE
Nov 10 at 23:55






Actually this is almost correct but not fully. It throws errors until you also put the <T> on the method call itself also: public static ObservableCollection<T> SQLAuthentication<T>(ObservableCollection<T> myCollection, T myClass, String sql) where T: class is what worked
– MattE
Nov 10 at 23:55




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243633%2fpassing-generic-class-to-amethod-that-returns-a-generic-observablecollection-of%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

Coverage of Google Street View

Full-time equivalent

Surfing