Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
In my database I have a Enum field. I want to put those Enum attributes in a List in C#.
I searched but I couldn't find the answer. Is this possible?
I use a MySqlDatabase. If I want to get the rows from the database I use:
using (var uow = new UnitOfWorkScope<TrackerEntities>(UnitOfWorkScopePurpose.Reading))
In my application I use the Entity Framework
if you loading data in a data reader, however this is sql-server way. But point here is to convert string into enum
var list = new List<YourEnumType>();
var field= reader["DBFieldName"] != DBNull.Value ? reader["DBFieldName"].ToString()
: "";
var myField=(YourEnumType) Enum.Parse(typeof(YourEnumType ), field);
list.Add(myField);
Also have a look at The ENUM Type
You'll need to make a query against the schema table to list the enum values.
SELECT COLUMN_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = #TABLE_NAME AND
COLUMN_NAME = #COLUMN_NAME
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Working on a legacy application where Data Access repository always returns a data set. Changing to any ORM frameworks is not an option for me at this point, looking for best options to map result set to a CLR type
I know of 2 easy ways to do this.
1 - Use Dapper.NET
const string query = "SELECT * FROM Users";
return connection.Query<User>(query);
With Dapper you don't even have to worry about getting the DataTable, just query the SqlConnection and get your type back.
2 - Use Automapper:
List<User> users = AutoMapper.Mapper.DynamicMap<IDataReader, List<User>>(
sourceDataTable.CreateDataReader());
Automapper can map your DataTable to your type.
Both methods require your class to have property names that match the column names in your source data.
I think they are both available as NuGet packages
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
In the following code I try to fill the combobox
while (myReader.Read())
{
string sName = myReader.GetString("profesor"); <--- here is the error
cb_1najprof.Items.Add(sName);
cb_2najprof.Items.Add(sName);
cb_3najprof.Items.Add(sName);
cb_1najsprof.Items.Add(sName);
cb_2najsprof.Items.Add(sName);
cb_3najsprof.Items.Add(sName);
}
When the code above is run I get the error cannot convert string to int on the indicated line.
You can try this..
string sName = myReader.GetString(myReader.GetOrdinal("profesor"));
or
string sName = myReader["profesor"].ToString();
Also see related question
How to get data by SqlDataReader.GetValue by column name
GetString takes an integer as an argument, not a string. You have called it with "profesor". Try calling it with eg 1 instead. This integer corresponds to the column you want to read from.
That's because you can't pass a string to the .GetString() method [documentation] You have to pass an int. The int (zero-based) is the column number you want to read.
Yes I know my documentation link is for a SqlDataReader, but the same concept applies for a MySqlDataReader
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a web service that returns the list of all the clients.
I can get the list into a gridview by calling the method get all data like this.
test.RH_WebServiceService ligar = new test.RH_WebService();
test.baseList[] data = ligar.getAllData();
The thing is I wanted to filter it by name (for example) I've been reading online and people have said to me that I can just do it like this:
test.baseList[] data = ligar.getAllData().Where(condition);
However I can't get it to work. Do you guys have any ideas?
Assuming you are using Linq then you can just do:
test.baseList[] data = ligar.getAllData().Where(d => d.Name == "John");
The d is a random letter given to the object. Name is what i am assuming your property is called. Although i would recommend creating a method in your service that you pass the name in and get back the filtered data. That way you only return data you need which will improve performance. Something like this:
test.baseList[] data = ligar.getDataByName("John");
Maybe this will help some:
test.baseList[] data = ligar.getAllData().Where(f=> f.field == "value").ToArray();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to put check to validate textbox that input value must not be similar to the values already present in database. Like:
if there is value with text "Hello" in database then user must not be allowed to save value either he writes:
Hello
HELLO
hElLo
HeLLO
Hello etc
I followed this http://www.dotnetperls.com/string-isupper-islower but as i am new to c# so have little confuse that how to match above defined words as all are same words Hello
I typically just convert both values (user input and stored value) to lower case when making the comparison.
Edit: if both values are in .NET, you could use String.Compare(s1, s2, StringComparison.OrdinalIgnoreCase)
Do you need to do this in code? I would suggest that you just make a unique constraint on the column and let the database handle that for you. Depending on the database you are using you may need to do a little additional work to handle the case sensitivity.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I try to group by StringId and then by Name
var a = myList
.GroupBy(item => new TcGroupByKey()
{StringId = item.Id, ChannelName = item.ChannelName}).ToList();
I have created this inner class TcGroupByKey
so I can pass the result to TrackingChannelRow ctor
and will get strongly type argument and not object
public TrackingChannelRow(ManageTcModel.TcGroupByKey tcGroupByKey,
IGrouping<ManageTcModel.TcGroupByKey, TrackingChannelItem> subChannels,
IEnumerable<Manager.TrackingChannels.Model.ToolbarItem> toolbars,
IEnumerable<Manager.TrackingChannels.Model.BundleItem> bundles)
{
But the group by doesn't work.
What am I doing wrong?
I think that what you're looking for is to group by composite key...
Something similar to what Marc described in this post Linq to SQL with Group by
The key is to use 'anonymous' types - not the named one like you did - that LINQ can translate into SQL, otherwise there's an issue and that code can only run 'post SQL' (so you enumerate first then group but lose on the SQL integration, performance).
So, in your case it'd be something like this...
new {StringId = item.Id, ChannelName = item.ChannelName}
...instead of new TcGroupByKey() {StringId = item.Id, ChannelName = item.ChannelName}
There is similar problem with doing a 'Select' into anonymous (works) or named type (doesn't - unless, as I mentioned above, you separate the SQL and C#-only expressions part, a bit simplified).
hope this helps
You are asking linq to group your list by an object he doesn't recognize.
What's wrong with:
var a = myList.GroupBy(Id).GroupBy(ChannelName).ToList();