Creating .json file [closed] - c#

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 have created JSON using following code.
List<int> _data = new List<int>();
foreach (DataRow row in dt.Rows)
{
_data.Add((int)row["Id"]);
}
JavaScriptSerializer jss = new JavaScriptSerializer();
chartData = jss.Serialize(_data);
Now can anyone tell me how do I save this is something like data.json? I want to write it into a file.

The Serialize method returns a String, doesn't it? So you can just use File.WriteAllText().

Related

Retrieve List<Object> to List<String> [closed]

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
Currently, I have a List<object> like this: List<USER>.
In User class, I have column named UserName.
I want to retrieve this column to List<string>.
How can it work without using foreach for List<USER>?
Thanks.
You can use LINQ:
List<string> userNames = users.Select(u => u.UserName).ToList();
In addition to Tim Schmelter post:
For casting IEnumerable<object> to IEnumerable<USER> you may use Cast<T> or OfType<T> method.
This can be written as follows:
var userNames = users.Cast<USER>.Select(u => u.UserName).ToList();

How can I convert DIA_User[] to List<DIA_User> [closed]

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 8 years ago.
Improve this question
I used wcf service to work directly to database.
And the result I got as this format: DIA_User[]
I can use linq for this as select, find,... but I dont know how to convert to List<DIA_User>
Please advise.
Thank you very much.
Just use ToList() method, which is an extension method on IEnumerable<>
DIA_User[] x = new DIA_User[] {};
List<DIA_User> y = x.ToList();
List<DIA_USER> users = new List<DIA_USER>(diaUserArray);
var arrayUser = new DIA_User[]();
var listUsers = arrayUser.ToList();
BTW, you can configure your WCF service to send Lists instead of Arrays.

Need to convert SQL to Linq-To-SQL [closed]

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 8 years ago.
Improve this question
select cast(' ' as varchar(1000)) as myfield, otherfield
from tab1;
I need to convert above SQL to Linq-To-Sql.
Maybe:
from item in dataContext.Items
select new { MyField = "", Item = item };
Unfortunately, AMAIK, there is no way to declare * in projection in LINQ. Thus to have all other fields alongside your MyField field, you should write them all.

Update DataTable records by Id in C# [closed]

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
Could anyone please suggest, I have a datatable with 100 records and want to update any 1 specific record based on id.
Here is an example, maybe it helps even if your reqirement is not that clear:
DataRow row = tableWith100Rows.AsEnumerable()
.FirstOrDefault(r => r.Field<int>("ID") == 4711);
if(row != null)
row.SetField("ColumnToUpdate", "new value");

How do I read the names of files in a directory into an array? [closed]

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 8 years ago.
Improve this question
I have a folder C:\MergeMe\ that is full of tab-delimited text files, but the number of files is unknown at any given time.
How do I read and store the names of the text files in the MergeMe directory into an array?
Use System.IO.Directory.GetFiles(). If you know the extension, you can, for example call:
Directory.GetFiles(#"C:\MergeMe\*.txt")
If you need other options, there are overloads that allow you to provide them.
static void Main(string[] args)
{
foreach (string file in Directory.GetFiles("MyPath"))
{
if (Path.GetExtension(file)=="youExtension")
{
using (StreamReader sr = new StreamReader(file))
{
//Your code
}
}
}
}

Categories

Resources