I have the following select result from SQL. I populate a List.
Name RequiredField Type Link
Product Status String NULL
Product Effective Date DateTime NULL
Product AccountId Guid Account
Product Name String NULL
Account LastName String NULL
Account Name String NULL
The order that it displays is relevant because i need an Account efore I can have a Product.
I need to generate a string from these lines in c# as follows.
I basically have to traverse and build a string something like:
StringBuilder str = new StringBuilder();
str.Append(Account a = new Account(););
str.Append(a.Name = 'Test';);
str.Append(insert a;);
// the link is (AccountId = a.Id)
str.Append(Product prod = new Product(AccountId = a.Id,Status='Draft',EffectiveDate = Date.today()););
str.Append(insert prod;);
I was looking at creating a model for each, Product and Account models and use that to create the string , but not sure how.
How can i create a model that uses recursion in order to create the string ?
StringBuilder str = new StringBuilder();
str.Append(Account a = new Account(););
str.Append(a.Name = 'Test';);
str.Append(insert a;);
// the link is (AccountId = a.Id)
str.Append(Product prod = new Product(AccountId = a.Id,Status='Draft',EffectiveDate = Date.today()););
str.Append(insert prod;);
I don't think you can build a string with above code and confused about what are you looking for.
robby32
I have the following select result from SQL. I populate a List.
I can see you can convert a datatable to list, that means you are already able to create a model to do something you want.
So, please provide more detailed description about your requirement and if possible your current code that might help us tell what you really want.
Now from that I know that the dependecy is that I must create an Acoount with properties of FirstName and LastName , I can set those values to lets say "John" as firstname and "Edwards" as lastname and from that I need to use those values that I set and
build a string as follows:(I use these string to send to another system )
string acctStr = "Account a = new Account(FirstName="John", LastName="Edwards"); a.insert "
Then read the Products and see that I need to create a Product model with properties Status , Effective Date, Name and AccountId, set some values and build a string.
lets say values are as follows. (They can be string )
Effectivbe Date = Date.Today();
Name = "Product Test"
Status = "Test_Status"
AccountId = a.Id <------ now this is the link to the Account I created earlier in the string above to get a string like
var productStr = "Product prod = new Product(AccountId = a.Id,Status='Test_Status',EffectiveDate = Date.today());"
and then make a universal string that outputs both in a single string like :
var finalStr = acctStr + Environment.NewLine + productStr.;
I was wanting to know how to build a class for the objects , Account and Products with their relative properties, populate the object and construct the output strings.
The list may vary , such that the names may be different . In other words I could have as follows
Name RequiredField Type Link
Order Name String NULL
Order Delivery Date String NULL
Order OrderId Guid Contact
Order Name String NULL
Contact Name String NULL
Contact Address String NULL
Once again I would create strings like above , but this time the model class will be for an Order and a Contact linked via the OrderId, whereby the Contact string is created first and then the Order.
I dont exactly know what will be under the Name column so its as if i need to dynamically create the model class with its properties as given in the list.
Can you simply explain the high level problem you are trying to solve rather than the solution? Are you trying to build dynamic objects from a database? My initial impression is you have a significant design issue.
Seems you want to create a class type with Name, RequiredField, Type and Link properties, populate a list of those items and send that to a web service? You don't send data to an API by creating C# code, instead JSON or XML is used behind the scene. See https://docs.microsoft.com/en-us/aspnet/web-api/
Or for now explain first what you are trying to do regardless of any technical consideration. For now it seems you try to implement a very unusual solution for some unclear problem (for now my undestanding is that you want to send data to a web service ?)
Edit: or your goal is really to create and compile C# code ????
yes in a nutshell build dynamic objects from the db
You'll need to explain the design if you want community support. As written, I don't see how it's possible to meet your requirement. It seems the result set contains type properties but there is no indication how properties are tied to a specific object
instance.
It seems like you are asking the community to solve design problem but you have not explained the intent.
Thankyou for the link , reading it helped in clarifying a few things; maybe i overcomplicated it. I had a look and basically creating some domain models will help in this scenario.
Member
281 Points
1002 Posts
How can i create a model class from the result of a sql query ?
Sep 09, 2019 06:13 AM|robby32|LINK
I have the following select result from SQL. I populate a List.
Name RequiredField Type Link
Product Status String NULL
Product Effective Date DateTime NULL
Product AccountId Guid Account
Product Name String NULL
Account LastName String NULL
Account Name String NULL
The order that it displays is relevant because i need an Account efore I can have a Product.
I need to generate a string from these lines in c# as follows.
I basically have to traverse and build a string something like:
StringBuilder str = new StringBuilder();
str.Append(Account a = new Account(););
str.Append(a.Name = 'Test';);
str.Append(insert a;);
// the link is (AccountId = a.Id)
str.Append(Product prod = new Product(AccountId = a.Id,Status='Draft',EffectiveDate = Date.today()););
str.Append(insert prod;);
I was looking at creating a model for each, Product and Account models and use that to create the string , but not sure how.
How can i create a model that uses recursion in order to create the string ?
Thanks
Contributor
3140 Points
983 Posts
Re: How can i create a model class from the result of a sql query ?
Sep 09, 2019 09:54 AM|Yang Shen|LINK
Hi robby32,
I don't think you can build a string with above code and confused about what are you looking for.
I can see you can convert a datatable to list, that means you are already able to create a model to do something you want.
So, please provide more detailed description about your requirement and if possible your current code that might help us tell what you really want.
Best Regard,
Yang Shen
Member
281 Points
1002 Posts
Re: How can i create a model class from the result of a sql query ?
Sep 09, 2019 11:02 AM|robby32|LINK
HI Yang
I return a list of objects as follows:
[0]
[0] "Product"
[1] "Status"
[2] "String"
[3] null
[1]
[0] "Product"
[1] "Effective Date"
[2] "String" <----- typo in previous post.
[3] null
[2]
[0] "Product"
[1] "AccountId"
[2] "Guid"
[3] "Account"
[3]
[0] "Product"
[1] "Name"
[2] "String"
[3] null
[4]
[0] "Account"
[1] "FirstName" <--- sorry typo in the first question i posted.
[2] "String"
[3] null
[5]
[0] "Account"
[1] "LastName" <
[2] "String"
[3] null
Now from that I know that the dependecy is that I must create an Acoount with properties of FirstName and LastName , I can set those values to lets say "John" as firstname and "Edwards" as lastname and from that I need to use those values that I set and build a string as follows:(I use these string to send to another system )
string acctStr = "Account a = new Account(FirstName="John", LastName="Edwards"); a.insert "
Then read the Products and see that I need to create a Product model with properties Status , Effective Date, Name and AccountId, set some values and build a string.
lets say values are as follows. (They can be string )
Effectivbe Date = Date.Today();
Name = "Product Test"
Status = "Test_Status"
AccountId = a.Id <------ now this is the link to the Account I created earlier in the string above to get a string like
var productStr = "Product prod = new Product(AccountId = a.Id,Status='Test_Status',EffectiveDate = Date.today());"
and then make a universal string that outputs both in a single string like :
var finalStr = acctStr + Environment.NewLine + productStr.;
I was wanting to know how to build a class for the objects , Account and Products with their relative properties, populate the object and construct the output strings.
The list may vary , such that the names may be different . In other words I could have as follows
Name RequiredField Type Link
Order Name String NULL
Order Delivery Date String NULL
Order OrderId Guid Contact
Order Name String NULL
Contact Name String NULL
Contact Address String NULL
Once again I would create strings like above , but this time the model class will be for an Order and a Contact linked via the OrderId, whereby the Contact string is created first and then the Order.
I dont exactly know what will be under the Name column so its as if i need to dynamically create the model class with its properties as given in the list.
Hope that clarifies a bit more.
Thanks
All-Star
53051 Points
23634 Posts
Re: How can i create a model class from the result of a sql query ?
Sep 09, 2019 11:12 AM|mgebhard|LINK
Can you simply explain the high level problem you are trying to solve rather than the solution? Are you trying to build dynamic objects from a database? My initial impression is you have a significant design issue.
Member
281 Points
1002 Posts
Re: How can i create a model class from the result of a sql query ?
Sep 09, 2019 11:15 AM|robby32|LINK
yes in a nutshell build dynamic objects from the db
All-Star
48530 Points
18075 Posts
Re: How can i create a model class from the result of a sql query ?
Sep 09, 2019 11:28 AM|PatriceSc|LINK
Hi,
Seems you want to create a class type with Name, RequiredField, Type and Link properties, populate a list of those items and send that to a web service? You don't send data to an API by creating C# code, instead JSON or XML is used behind the scene. See https://docs.microsoft.com/en-us/aspnet/web-api/
Or for now explain first what you are trying to do regardless of any technical consideration. For now it seems you try to implement a very unusual solution for some unclear problem (for now my undestanding is that you want to send data to a web service ?)
Edit: or your goal is really to create and compile C# code ????
All-Star
53051 Points
23634 Posts
Re: How can i create a model class from the result of a sql query ?
Sep 09, 2019 11:35 AM|mgebhard|LINK
You'll need to explain the design if you want community support. As written, I don't see how it's possible to meet your requirement. It seems the result set contains type properties but there is no indication how properties are tied to a specific object instance.
It seems like you are asking the community to solve design problem but you have not explained the intent.
Member
281 Points
1002 Posts
Re: How can i create a model class from the result of a sql query ?
Sep 09, 2019 11:46 AM|robby32|LINK
Hi PatriceSc,
Thankyou for the link , reading it helped in clarifying a few things; maybe i overcomplicated it. I had a look and basically creating some domain models will help in this scenario.
Thanks