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.
Related
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();
This question already has answers here:
Using contains() in LINQ to SQL
(7 answers)
Closed 8 years ago.
I am trying to convert the following SQL statement to a Link2SQL statement.
SELECT * FROM Global.CustomData
WHERE CustomDataSource LIKE '%Plugin%'
I have converted it to this statement
var query =
from item in db.CustomDatas
where item.CustomDataSource.Contains(dataSource)
select item;
And have tried setting dataSource to the following: "Plugin", "%Plugin%", "/Plugin/" and "%/Plugin%/". These I have taken from other examples. Unfortunately, although the TSQL statement does return a value, I cannot get the Linq2Sql statement to return anything. Could someone tell me what I am doing wrong?
You should pass "Plugin", the only thing I can think of is the case sensitivity. Try something like this:
where item.CustomDataSource.ToLower().Contains(dataSource.ToLower())
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
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
This question already has an answer here:
Closed 13 years ago.
Possible Duplicate:
Disjoint Union in LINQ
DUPE: Disjoint Union in LINQ
I know this is a simple collection operation,My code is:
var gone = from a in A
where B.Contains(a) == false
select a;
but it not work.
var gone = A.Except(B);