c# 2D array - merging Values [closed] - c#

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 2 years ago.
Improve this question
I have a 2 Dimensional string array(5*2) like this:
string[,] data= new string[5, 2] {{"F1","LINK1"},
{"F1","LINK2"},
{"F2","LINK3"},
{"F3","LINK4"},
{"F3","LINK5"}};
i want to group and merge the values into new array.
Output :
{"F1","LINK1,LINK2"},
{"F2","LINK3"},
{"F3","LINK4,LINK5"}
The output array 3*2.

Something like that:
string[,] data= new string[5, 2] {{"F1","LINK1"},
{"F1","LINK2"},
{"F2","LINK3"},
{"F3","LINK4"},
{"F3","LINK5"}};
var items = Enumerable.Range(0, data.GetLength(0))
.Select(n => new {Key = data[n, 0], Link = data[n, 1]});
var query =
from i in items
group i by i.Key
into g
select new []
{
g.Key,
string.Join(",", g.Select(x => x.Link))
};
var result = query.ToArray();

Related

ASP.NET using Entity Framework display data to drop down list with distinct [closed]

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 2 years ago.
Improve this question
Please I need to implement distinct in a.CompanyName but it doesn't work. What do I need to do ?
try
{
var listCompany = company.CompanyList();
companyList1.DataSource = listCompany.Select(a => new { a.CompanyName, a.CompanyId }).Distinct();
companyList1.DataTextField = "CompanyName";
companyList1.DataValueField = "CompanyId";
companyList1.DataBind();
}
catch (Exception)
{
throw;
}
You can use with .GroupBy()
Groups the elements of a sequence.
companyList1.DataSource = listCompany
.GroupBy(x => x.CompanyName)
.Select(a => new
{ CompanyName = a.First().CompanyName, CompanyId = a.First().CompanyId });
Above query will group your result by company name and then will select CompanyName and CompanyId of first record
Try out online

In a string list how to split by every 2 character and insert a comma [closed]

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 4 years ago.
Improve this question
I have a list string which has values like below, could you please let me know how to split the list by every 2 character and insert a comma separated and assign the final list to another list.
var list1 = new List<string>() {"DVMNKL"};
var list2 = new List<string>() {"DV","MN","KL"};
Some time list1 can have only 2 character, at that time I should not split, I have to just assign to list2
You can use System.Linq to manage that.
int splitByCount = 2;
string s = new List<string> { "DVMNLS", "DVMNLS" };
var split = s.SelectMant(c => c) //flatten the list of strings to IEnumerable<char>
.Select((c, index) => new {c, index})
.GroupBy(x => x.index/splitByCount)
.Select(group => group.Select(elem => elem.c))
.Select(chars => new string(chars.ToArray()));
Console.WriteLine(string.Join(",", split));
the output
DV,MN,KL,DV,MN,KL

how to sort date in c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have many date in one variable like:
var date = hfAllDates.Value.Split('#');
there are four date(01/01/2016,22/01/2016,18/01/2016,05/01/2016) in date variable .
how to sort date.
Thanks
add namespace using System.Linq;
and use LINQ Query
in ascending order
var orderedDateList = date.OrderBy(x => DateTime.Parse(x)).ToList();
in Descending oreder
var orderedDateList = date.OrderByDescending(x => DateTime.Parse(x)).ToList();
Convert date from string[] to List<DateTime> first:
List<DateTime> dtList = date.Select(x => DateTime.Parse(x)).ToList();
Then you can easily sort it:
dtList.Sort();
Using Linq's OrderBy:
var sortedDates = date.OrderBy(x => DateTime.Parse(x));
or
var sortedDates = date.OrderByDescending(x => DateTime.Parse(x));
if you are needed to sort dates descending
I suggest using Linq:
var date = hfAllDates.Value.Split('#')
.Select(line => DateTime.Parse(x))
.OrderBy(x => x)
.ToList();

How to re-write this linq from query syntax to method syntax? [closed]

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
As in title, how to rewrite this linq query into method syntax?
(from row in recordsEpP
where (!string.IsNullOrEmpty(row.Column20)
&& !string.IsNullOrEmpty(row.Column14)
&& string.IsNullOrEmpty(row.Column8))
select new ReportDto{
Status = "P",
ContractNumber = row.ContractNumber,
Count = 1
}).ToList();
You could try something like this:
recordsEpP.Where(row=> !string.IsNullOrEmpty(row.Column20) &&
!string.IsNullOrEmpty(row.Column14) &&
string.IsNullOrEmpty(row.Column8)
).Select(row => new ReportDto{
Status = "P",
ContractNumber = row.ContractNumber,
Count = 1
}).ToList();

populating IEnumerable collection with a List<string> data [closed]

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 9 years ago.
Improve this question
I have an existing List that countains data
List<string>countries
I want to somehow loop through this data an populate the following collection
IEnumerable<SelectListItem> Countries
How would I go about in doing this?
The simplest method I can think off is using a Linq query:
Countries = countries.Select(country => new SelectListItem {Text = country, Value = country});
You won't have the ability to differ the text from the value, however.
Presuming this is related to mvc... try this
IEnumerable<SelectListItem> Countries = countries.Select(c => new SelectListItem { Text = c, Value = c });
Something like this I think.
IEnumerable<SelectListItem> Countries = countries.Select(c => new SelectListItem
{
Text = c,
Value = c
});

Categories

Resources