How To Sum NoofHours in SQL SERVER [duplicate] - c#

This question already has answers here:
How can I get the sum of multiple datetime values?
(2 answers)
Closed 8 years ago.
i have a table which contain a column NoofHours and data type is in datetime now i want to sum the values like 9:30 + 9:45 = 19:15(HH:MM) Format kindly Help.

Select sum(NoofHours) as sumO
FROM YourTable
Group By NoofHours

this should work for you:
SELECT convert(time,DATEADD(hh,SUM(DATEPART(hh,CONVERT(DATETIME,tm,108))), DATEADD(n,SUM(DATEPART(n,CONVERT(DATETIME,tm,108))),'1900.01.01') )) FROM temp
I've also created a SQLFiddle for you to test with SQLFIDDLE HERE

Related

Query a list and select top 10 values [duplicate]

This question already has answers here:
How to get first N elements of a list in C#?
(7 answers)
Closed 4 years ago.
I have a list of objects in which each object has a property called "Frequency" and I want to be able to pick the top 10 objects that have the highest frequencies.
I saw some solutions that are kind of similar to what I am looking to solve using LINQ so any help is appreciated.
You can order the list by descending Frequency and then take the first 10 like this:
var top10 = objectList.OrderByDescending(o => o.Frequency).Take(10);

Running a Distinct on a record [duplicate]

This question already has answers here:
Distinct in Linq based on only one field of the table
(10 answers)
Closed 7 years ago.
In LINQ if the result that I have is the a list of whole records from the database and I want to do a Distinct on one column of this record, How do I do that?
You could try something as simple as:
var distincts = records.Select(x=>x.ColumnName).Distinct();

Select 50 to 100 rows from datatable [duplicate]

This question already has answers here:
how to get second record in linq to sql
(9 answers)
Closed 8 years ago.
I need to select rows 50 to 100 from a datatable. I have tried for the first 50 rows using this code:
dt.Rows.Cast<System.Data.DataRow>().Take(50)
Now I need rows 50 to 100 from the datatable. How do I do this?
Use Skip method
dt.Rows.Cast<System.Data.DataRow>().Skip(50).Take(50)

Getting a value from datatable [duplicate]

This question already has answers here:
Filling data at a particular location in a Data Table in C#
(3 answers)
Closed 8 years ago.
I have a Datatable like this.Now how can i pick the value from zones based on weight and zone.I am doing this in c#
Using LINQ Query : You need to use the AsEnumerable() extension for DataTable
var results = from myRow in myDataTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;
AsEnumerable() returns IEnumerable. If you need to convert IEnumerable to a DataTable, use the CopyToDataTable()
Check Here
If you want to pick cells/rows with criteria. you need to a select clauase in your datatable. Go Here to see some examples

"Between" in Linq C# [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
LINQ Between Operator
Dear All,
Hi,
I need to write this query in LINQ C#. can anyone help me?
Select *
From Mytable
where MyText BETWEEN 'john' AND 'Pear'
I believe this query should work:
var results = yourTable.Where(x => x.Text.CompareTo("john") > 0 &&
x.Text.CompareTo("Pear") < 0);
This assumes that you want to compare the text in each row of the table, and not some pre-dfined string.
Here is how you can do it with ObjectQuery
MytableSet.Where("it.Name between #start and #end", new ObjectParameter("start", "john"), new ObjectParameter("end", "Pear"))
EDIT:
Forget to mention that this statement is specific to Entity Framework not LINQ2SQL.

Categories