Rewrite SQL to LINQ query - linq

Hi I am trying to write a SQL query to LINQ query. My SQL query is like this:
select BookingArrivedEnquiredTime
from BookingArriveds
where BookingArrivedEnquiredDateTime='2015-02-17 00:00:00.000'
order by CAST(('01/01/2000 ' + BookingArrivedEnquiredTime) AS DATETIME)
How can I write this query in LINQ?

You might try just parsing the string as DateTime:
var res = from item in BookingArrivedEnquiredTime.BookingArriveds
where item.BookingArrivedEnquiredDateTime == DateTime.Parse("2015-02-17 00:00:00.000")
orderby DateTime.Parse("01/01/2000 " + item.ToString())
select item;

Related

LINQ query NOT IN

I have this simple SQL query using NOT IN operator:
SELECT * FROM List l WHERE L.ListName NOT IN ('POType','MailClass')
How can I write this using LINQ query syntax?
string[] listNames = {"POType", "MailClass"};
var query = from l in List
where ! listNames.Contains(l.ListName)
select l;

I want to execute my linq query which is in string format. Is there any way to execute string as linq to sql

Is there any way to execute string as linq to sql query like my query is
var anothertest = " where dri.routeId=1";
var test = "from dri in applicationDbContext.DeliveryRequestItems"+ anothertest + "select dri";
how to execute this query take result from table.

ASP.net LINQ on DataView to use Like query

I want to filter a DataView but DV.Rowfilter is taking too much time .
dv.RowFilter = "ProductName like '%" + searchtxt + "%'";
So I decided to use the LINQ but how to implement above like query in LINQ ?
LINQ is not more efficient in general, but it can increase readability and maintainability.
So you can try Linq-To-DataSet:
var query = from row in dv.Table.AsEnumerable()
let productName = row.Field<string>("ProductName")
where productName.Contains(searchtxt)
select row;
DataTable tbl = query.CopyToDataTable(); // use this as DataSource or use tbl.DefaultView
Here the same with method syntax:
var query = dv.Table.AsEnumerable()
.Where(row => row.Field<string>("ProductName").Contains(searchtxt));
MSDN: Creating a DataView Object with LINQ to DataSet
i have tried your second solution, but now its throwing the exception
"The source contains no DataRows." and actually the DataTable which i
make as DataTable.AsEnumerable() , it has the rows in it
The table contains rows but the filter skips all of them.
You could use if(query.Any()){...} to check if there are rows:
DataTable tbl = dv.Table.Clone(); // creates an empty table with the same columns
if(query.Any())
tbl = query.CopyToDataTable();

how to implement sql like casting selection in lambda expressions

how to convert this sql statement into linq or lambda expression
SELECT Month,Year FROM Tbl_OrderDetails Order by cast(Year as int) ,cast(Month as int)
Here Month and Year are in VARCHAR type
Thanks all,
try this
var skata = from ordtls in Tbl_OrderDetails
orderby (int)ordtls.Month, (int)ordtls.Year
select new {ordtls.Month,ordtls.Year}
or
var skata = from ordtls in Tbl_OrderDetails
orderby Convert.ToInt32(ordtls.Month), Convert.ToInt32(ordtls.Year)
select new {ordtls.Month,ordtls.Year}

how to write this oracle query in jpa?

Query:
select *
from easquestionsinfo
where questionname in(select questionname
from easresponseinfo
where isconflict = 'yes')
This query works fine and returns me the records from table 'easquestioninfo' when questionname is equal to the one returned by the inner query which returns set of questionname where isconflict='yes'.
JPA supports JPQL, SQL, and Criteria.
You can execute this SQL directly using createNativeQuery().
For JPQL, it depends on your object model, perhaps something like,
Select q fom QuestionInfo q where q.name in (Select r.name from ResponseInfo q2 where r.isConflict = 'yes')
See,
http://en.wikibooks.org/wiki/Java_Persistence/JPQL

Resources