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();
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 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();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have the following code in MySQL
SELECT Query,SUM(TotalResults)
FROM TotalJobResults
GROUP BY Query
ORDER BY `SUM(TotalResults)` DESC
I am trying to duplicate this function calling a MongoDB Collection in C# and cannot seem to actually understand it very well. I have the code below but it seems far from answering the problem,
var t = from q in _jobCollection.Find(_ => true).ToList().GroupBy(
p => p.Query,
p => p.TotalResults,
(key, g) => new { Query = key, Total = g.ToList().Sum() }
);
Thanks for any light you can shed on this!
Looks like it works if I put a select q on the end. hmm! my error
var t = from q in _jobCollection.Find(_ => true).ToList().GroupBy(
p => p.Query,
p => p.TotalResults,
(key, g) => new { Query = key, Total = g.ToList().Sum() }
) select q;
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
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 the following query in sql server
select COUNT(mc_owner) as nbr ,
case mc_owner when 'Element1' then 'Element1' else 'others' end Owner
from [dbo].[full]
where (date_reception > '01-01-2015')
group by (CASE mc_owner WHEN 'Element1' THEN 'Element1' ELSE 'others' END)
order by nbr desc
I need to convert it to entityframework linq query
Try this,
var result = fulltableData.Where(x=>x.date_reception > '01-01-2015')
.GroupBy(x=> x.mc_owner.Equals("Element1")?"Element1":"others")
.Select(x=> new{ nbr = x.Count(), Owner = x.Key})
.OrderByDescending(x=>x.nbr);
I understand you want to select the number of occurrences of a certain mc_owner compared to all others. The following LINQ query should produce the same result:
full.Where(f => f.date_reception > new DateTime(2015, 1, 1))
.GroupBy(f => f.mc_owner == "Element1" ? f.mc_owner : "others")
.OrderByDescending(group => group.Count())
.Select(group => new { Owner = group.key, nbr = group.Count());
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 8 years ago.
Improve this question
How can I convert this query syntax to method syntax in linq:
return (from x in db.Table1
join y in db.Table1 on x.ID equals y.ID - 1
where Convert.ToInt32(y.ID) >= Convert.ToInt32(x.ID)
orderby x.Name
select x).Distinct();
Which approach is better? I like this query approach better but I was asked to work with method syntax which looks too bloated to me.
var results = db.Table1.Join
(
db.Table1,
x=>x.ID,
x=>x.ID - 1,
(x,y)=>new{OuterTable = x, xid = x.ID, yid = y.ID}
)
.Where(x=>Convert.ToInt32(x.yid ) >= Convert.ToInt32(x.xid))
.Select(x=>x.OuterTable)
.OrderBy(x=>x.Name)
.Distinct();