Linq to SQL order by Distinct(Linq to SQL order by with Distinct)
我的环境:VS 2013 Express中的ASP.net和C#。
我已经通过许多类似的SO文章试图解决这个问题。 我是Linq to SQL查询和c#的业余爱好者。
我正在尝试使用Linq to SQL从列中获取前5个最新的不同值,然后将它们添加到列表中。 我的应用程序是使用c#的asp.net和用于数据抽象的.dbml文件。
我尝试了很多不同的方法。 我要么得到非明确但排序的列表,要么我得到一个明确的未排序列表。 到目前为止我的内容如下
var Top5MFG = (from mfg in db.orders where mfg.manufacturer.Length > 0 && mfg.customerid == "blahblahblahblahblah"<br /> select new { manufacturer = mfg.manufacturer, date = mfg.date_created }) .Distinct() .OrderByDescending(s => s.date);
我认为我的“Distinct”正在查看“ID”列,也许我需要告诉它我希望它看看“制造商”专栏,但我还没有弄清楚如何/如果它可以做那。
我可以通过使用storedproc轻松地完成这项工作,但是如果可能的话,我真的想直接使用c#代码。 这是我发给SO的第一篇文章,我希望我能把它正确地放在一起。 任何帮助非常感谢。
谢谢
My Environment: ASP.net and C# in VS 2013 Express.
I have been through many similar SO articles trying to work this out. I am amateur with Linq to SQL queries and c# in general.
I'm trying to use Linq to SQL to get the top 5 most recent distinct values from a column, then add them to a list. My application is asp.net using c# and a .dbml file for data abstraction.
I've tried it many different ways. I either get non-distinct yet sorted list, or I get a distinct unsorted list. What I have so far is below
var Top5MFG = (from mfg in db.orders where mfg.manufacturer.Length > 0 && mfg.customerid == "blahblahblahblahblah"<br /> select new { manufacturer = mfg.manufacturer, date = mfg.date_created }) .Distinct() .OrderByDescending(s => s.date);
I'm thinking my "Distinct" is looking at the "ID" column, and perhaps I need to tell it I want it to look at the "manufacturer" column, but I haven't worked out how / if it's possible to do that.
I could do this with ease by using a storedproc, but I'm really trying to do it with c# code directly if possible. This is my first post to SO, I hope I have put it together properly. Any help much appreciated.
Thanks
原文:https://stackoverflow.com/questions/26068277
最满意答案
The
sqlite3
command-line shell is intended to be used interactively, so it allows you to continue after an error.To abort on the first error instead, use the
-bail
option:sqlite3 -bail my.db < mwe.sql
相关问答
更多-
摘自博客 : 当您的交易达到16级错误时,交易可能会进入注定状态(XACT_STATE()= -1),但并不总是如上所述!! 以下是博客中给出的一些例子: --datatype conversion errors: DECLARE @GUID UNIQUEIDENTIFIER SELECT @GUID = CONVERT(UNIQUEIDENTIFIER, 'ABC') ---divide by zero errors: SELECT * FROM my_books WHERE ...
-
我找到了答案。 为了使事务在出错时回滚,在基于CORDOVA的HYBRID APP中使用SQLITE时, 必须遵循以下任何一个选项 : 如果确实提供了错误回调,请确保它返回true。 如果错误回调返回false,则事务将不会回滚,并且先前的语句将自动提交。 db.transaction(function(tx) { tx.executeSql("DROP TABLE if exists Table1Backup", [], success, error); tx.executeSql("CREATE TAB ...
-
如果你不打电话, stmt.getConnection().commit() 代替 stmt.execute("COMMIT"); Should you not call, stmt.getConnection().commit() instead of stmt.execute("COMMIT");
-
出于某种原因,我能够以最奇特的方式解决这个问题。 我未安装ImageMagick,Paperclip的依赖项,并突然一切工作? 我绝对无法解释为什么,但嘿,它的工作......显然不是最好的答案,为什么会发生这种情况,但至少我现在可以继续前进。 我希望这个问题在转向生产时不会再出现。 For some reason, I was able to fix this issue in the most peculiar way possible. I UNINSTALLED ImageMagick, a dep ...
-
sqlite3命令行shell旨在以交互方式使用,因此它允许您在发生错误后继续。 要改为中止第一个错误,请使用-bail选项: sqlite3 -bail my.db < mwe.sql The sqlite3 command-line shell is intended to be used interactively, so it allows you to continue after an error. To abort on the first error instead, use the -b ...
-
这是我在我的存储过程中使用的一个对我有用的事务。 DECLARE exit handler for sqlexception BEGIN -- ERROR SHOW ERRORS; ROLLBACK; END; DECLARE exit handler for sqlwarning BEGIN -- WARNING SHOW WARNINGS; ROLLBACK; END; 更多信息可以在这里找到 http://w ...
-
sqlite3_exec()将在遇到的第一个错误中止,大多数错误不会导致自动回滚。 您应首先执行BEGIN,然后执行事务中的所有内容,然后使用COMMIT或ROLLBACK结束事务。 在ROLLBACK的情况下,您可能只是忽略任何错误。 转换已经回滚,或者你无论如何都无法做到。 sqlite3_exec() will abort in the first error encountered, an most errors do not result in an automatic rollback. You ...
-
尝试将savepoint_name与ROLLBACK语句一起使用,如下所述: 如果没有此保存点,ROLLBACK语句将按事项将事务回滚到最外面的BEGIN TRANSACTION语句。 ROLLBACK { TRAN | TRANSACTION } [ transaction_name | @tran_name_variable | savepoint_name | @savepoint_variable ] [ ; ] 没有savepoint_name或transaction_n ...
-
SQL语句中的SQLITE中的错误处理和RollBack事务(Error handling and RollBack Transaction in SQLITE from SQL Statement)[2024-02-13]
请查看关于冲突条款的SQLite 从BEGIN TRANSACTION开始 你必须在你的行动中添加ON CONFLICT ROLLBACK 最后和COMMIT TRANSACTION ;-) Please take a look at SQLite on conflict clause Start with BEGIN TRANSACTION You have to add ON CONFLICT ROLLBACK on your actions And COMMIT TRANSACTION at the ... -
连接一次只能有一个事务。 因此,如果要将多个线程彼此隔离,则必须为每个线程使用一个连接,或者将数据库外部的线程序列化。 A connection can have only a single transaction at a time. So if you want to isolate multiple threads from each other, you have to use one connection for each of them, or serialize the threads out ...