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 7 years ago.
Improve this question
How would I convert this SQL query to Linq?
Declare #Date Date
select #Date = Max (InsertedDate) from Rates
SELECT * FROM Rates where CAST(InsertedDate AS Date) =CAST(#Date AS Date) ORDER BY InsertedDate DESC
Your Sql Query appears to find all records on the last inserted date. Your Sql is approximately equivalent to:
var lastDate = db.Rates.Max(r => r.InsertedDate).Date;
var allRatesOnTheLastDate = db.Rates.Where(r => r.InsertedDate >= lastDate)
.OrderByDescending(r => r.InsertedDate);
This should be more performant than your Sql, given that you are casting the InsertedDate column which will prevent SARGability of any index on the column.
(Assuming an EF or Linq2Sql context called db with a Rates entity bound to it)
Related
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 1 year ago.
Improve this question
When I execute following query , it is executed properly in SSMS but same query in code does not delete anything and 0 rows are affected from thousands of rows.
Code is excuted without any error , have tried Query parameters as well but not worked.
Logs is table and DateCreated is column of DateTime.
Sql Connection settings and other things are correct.
SSMS query execution below :
Can someone please correct this query for code ?
connection.Open();
SqlCommand cmd1 = new SqlCommand("delete TOP (100) from [Logs] WHERE DateCreated < GETDATE() - 30", connection);
var number_of_rows_deleted = cmd1.ExecuteNonQuery();
result = number_of_rows_deleted.ToString() + " records deleted";
When TOP is used with DELETE, the referenced rows are not arranged in any order and the ORDER BY clause can not be directly specified in this statement. If you need to use TOP to delete rows in a meaningful chronological order, you must use TOP together with an ORDER BY clause in a subselect statement
your sql script should be like this
DELETE from [Logs]
WHERE Id IN
(SELECT TOP(100) Id
FROM Logs
WHERE DATEDIFF(day, DateCreated, GETDATE()) < 30
Order By DateCreated )
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 1 year ago.
Improve this question
In my database the date and time appear together with the data that was placed, but if I filter by date in C# as it has the time together I can't filter by date. Do I have to create a field for the date and another for the time?
...
WHERE med_date > '2021-06-08'
will get you all values that are after midnight at the start of today.
If you want to compute daily statistics then use CAST(... AS date) such as
SELECT CAST(med_date AS date) AS med_date, COUNT(*) AS n
FROM TheTable
GROUP BY CAST(med_date AS date)
ORDER BY 1
If you're using entity framework and LINQ to SQL queries, you can filter for date this way. Is that what you looking for?
dbcontext.DbSet<YouerEntity>.Where(x=>x.med_data.Date >= Date)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I need to select a product for a user based on other data in the database.
If the data is filtered out on the database that will require less data to be send to the server.
User (Id)
Product (code)
Access (User_Id, code) // Matching users to object codes
Will this query execute on the database sending back the minimal amout of data?
var products = QueryOver.Of<Access>()
.Where(a => a.User_Id == User.Id())
.Select(Projections.Property<Acces>(a => a.Code));
var access = QueryOver.Of<Product>()
.WithSubquery.WhereProperty(h => h.Code)
.In(products)
.Future();
This is very reasonable way how to filter data. The result of your queries would look like one SELECT against the DB:
SELECT ...
FROM Product
WHERE Code IN (SELECT Code FROM Access WHERE UserId = #userId)
So, this will for sure be executed on the DB Server, less data will be transfered, and what's more, it also would allow you to do the correct paging (if needed) - this scenario is the way how to filter parent over its one-to-many relations (find Parents which child has...)
Maybe check these Join several queries to optimise QueryOver query, NHibernate QueryOver - Retrieve all, and mark the ones already "selected"
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am currently trying to display a record in my form with a sql database table called "Registration" 2 column. Information will be display in gridview
Below is the table:
Registration ID Date Registered
1 10/4/2013
2 11/4/2013
3 12/5/2013
4 13/6/2013
Result will be:
Number of record Month Registered
2 April
1 May
1 June
Do anyone know what the query is?
Try this :
Select Count(*) As NumberOfRecord, DATENAME(month, DateRegistered) As MonthRegistered
from TableName GRoup by DATENAME(month, DateRegistered)
try this
select count(*) as NumberOfRegister,DATENAME(MONTH,DateRegisterd) as MonthRegistered
from table_name
group by DATENAME(MONTH,DateRegisterd)
you can try SQL something like below to retrive data
select count(*) as Numberofrecord, datename(month, DateRegistered) as MonthRegistered
from table
group by datename(month, DateRegistered)
If you want to get records from SQL, then try something like this
SELECT MONTH(DateRegisterd) MONTH, COUNT(*) COUNT
FROM YourTableName
WHERE YEAR(DateRegisterd)=2013//If you want records of particular year
GROUP BY MONTH(DateRegisterd)
OR like this
select count(*) as Numberofrecord, datename(month, Date_Registered) as MonthRegistered
from YourTable group by datename(month, Date_Registered)
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
Can someone please break this c# down in english? Is it possible to add a join in there somehow?
return db.Providers.Where(n => n.LastName.StartsWith(prefixText)).OrderBy(
n=>n.LastName).Select(n => n.LastName).Take(count).ToArray();
Thanks.
The SQL query is probably something like:
SELECT LastName FROM Providers
WHERE LastName LIKE 'PrefixText%'
ORDER BY LastName
LIMIT count; -- This may be TOP in MS SQL or ROWNUM in Oracle
Which means:
Give me all rows from the table Providers where the LastName column starts with PrefixText (whatever that variable contains). I want them sorted alphabetically by the LastName column, and I only want the first count rows (i.e., if count was equal to 50, you'd get up to 50 rows)
Sure, you can do a JOIN. You can refer to another table within your Where expression:
db.Providers.Where(n => n.ProviderGroup.ADgroup == 'Active Dir Group')
And the framework will automatically join in ADgroup for you, provided your model provides the necessary relationships between your tables.
Get x number of last name's of all Providers whose LastName starts with the text in the variable prefixText, in ascending alphabetical order.
This will return an array containing count last names, ordered in increasing alphabetical order, and starting with prefixText.
This is the corresponding SQL code:
select top #count LastName from Providers
where LastName like '+#prefixText+%'
order by LastName
Why do you need a join here?
Update:
Per the OP comment:
I need a join for limit the results of an Ajax auto extender...
You don't need a join for limiting the results in the Ajax Auto Extender, just use SQL's top clause or LINQ's Take method, just like you're doing right now:
db.Providers.Where(n => n.LastName.StartsWith(prefixText)).OrderBy(
n=>n.LastName).Select(n => n.LastName).Take(count).ToArray();