从sql询问linq [英] asking from sql to linq
问题描述
select ID,AmountFrom,AmountTo,DiscountPercent
frOM Discount where 2000 between AmountFrom and AmountTo
order by DiscountPercent desc
above如何在上面的语句中使用linq编写以从数据表中检索
请帮帮我!!!!!!!
့how to write with linq above statament to retrive from data table
pls help me!!!!!!!
推荐答案
我会尝试:
I would try:
dim q = (from d in Discount
where (d.AmountFrom >= 2000 andalso d.AmountTo <= 2000)
order by d.DiscountPercent descending
Select d.ID, d.AmountFrom, d.AmountTo, d.DiscountPercent).ToList()
或(以db为上下文)
or (db being the context)
dim q = from db.Discount.Where(function(d) (d.AmountFrom >= 2000 andalso d.AmountTo <= 2000))
参考: http://stackoverflow.com/questions/1414893/linq- to-sql-value-between-double-double-values [ http://www.linqpad.net/ [ ^ ]
ref: http://stackoverflow.com/questions/1414893/linq-to-sql-value-between-two-double-values[^]
Oh, yeah can I really recommend a tool that will help massivly with Linq stuff!!!
LinqPad!!!!
http://www.linqpad.net/[^]
如果您已经有一个包含数据的数据表,则此VB.NET代码应该可以工作:
If you have a DataTable with that data in it already, this VB.NET code should work:
Dim myData = From d In myDataTable.AsEnumerable() _
Where d.AmountFrom > 2000 AndAlso d.AmountTo < 2000
Select d _
Order By d.DiscountPercent Descending
请注意,您必须使用AsEnumerable
扩展方法,该方法包含在System.Data.DataSetExtensions
类中.您可以在此处找到有关此内容的更多信息:
http://stackoverflow.com/questions/10855/linq-query-on-a-datatable [ ^ ]
如果您想直接使用Linq2Sql(我建议您不要使用ADO.NET来获取DataTable,本文将为您提供所需的所有帮助:
在Visual Basic中使用LINQ to SQL [ ^ ]
还要注意,Where子句有点粘.我不确定您到底想做什么,所以我把它弄清楚了.您可以根据需要对其进行进一步的修改.
Note that you have to use the AsEnumerable
extension method, which is included in the System.Data.DataSetExtensions
class. You can find out more about this here:
http://stackoverflow.com/questions/10855/linq-query-on-a-datatable[^]
If you want to use Linq2Sql directly (which I would recommend instead of using ADO.NET to get a DataTable, this article will give you all the help you need:
Using LINQ to SQL in Visual Basic[^]
Also note that the Where clause is a bit sticky. I wasn''t sure what you wanted to do exactly so I got it close. You can further modify it as you need.
这篇关于从sql询问linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!