Retrieve List<Object> to List<String> [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
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();

Related

Creating .json file [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
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().

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.

LINQ Split list into lists by date [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
I have list of objects List<Payment>. I would like to get list of lists grouped by PaymentDate. Something like that: List<List<Payments>> where each list of the list have same date (without hours).
DateTime has a Date property:
List<List<Payments>> result = payments
.GroupBy(p => p.PaymentDate.Date)
.Select(g => g.ToList())
.ToList();
You can either use GroupBy as suggested by others, or you can just create a Lookup:
var lookup = payments.ToLookup(payment => payment.PaymentDate.Date);
You can iterate over that (using each group's key as the date) or fetch all the payments for a given date using the indexer. I usually find that a lookup is cleaner than using a nested collection such as a List<List<...>> or a Dictionary<..., List<...>>.
You can do this using GroupBy(). The result will be of type List<List<Payment>>.
var result = payments.GroupBy(payment => payment.PaymentDate.Date)
.Select(group => group.ToList())
.ToList()

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

Categories

Resources