I have a news portal.
我有一个新闻门户网站。
For this portal I have a database with a "News" table and with the following columns (NewsID, CategoryID, NewsTitle, NewsText, DateAdded, ImagePath, TotalRead, NewsType, isActive)
对于这个门户网站,我有一个带有“新闻”表和以下列的数据库(NewsID,CategoryID,NewsTitle,NewsText,DateAdded,ImagePath,TotalRead,NewsType,isActive)
I use dataset files (.xsd) and for this one, I have a query that returns the last 3 days' news into a custom class that I coded, named HHNews.
我使用数据集文件(.xsd),对于这个,我有一个查询,将最近3天的新闻返回到我编码的自定义类,名为HHNews。
HHNews Class has a function that returns a strongly-typed datatable that includes the results of the query I mention above.
HHNews类有一个函数,它返回一个强类型数据表,其中包含我上面提到的查询结果。
The home page has different sections for news.. these are; - Headlines (5 items) - Sub-headings (4 items) - Last 5 news items for each of the news categories...( categories are like; sports, local news, economics,
主页有不同的新闻部分..这些是; - 标题(5项) - 小标题(4项) - 每个新闻类别的最新5个新闻项目......(类别如:体育,本地新闻,经济学,
For the home page, I retrieve the datatable returned from the class. Now I want to query this datatable and build the sections I mention above.. e.g.
对于主页,我检索从类返回的数据表。现在我想查询这个数据表并构建我上面提到的部分..例如
if my datatable is called "dt", then is there a way to sql-like query this dt such as "select TOP(5) NewsID, NewsTitle, NewsText from dt where NewsType = 0" -- 0 representing the headline ?
如果我的数据表被称为“dt”,那么有没有办法像sql一样查询这个dt,例如“选择TOP(5)NewsID,NewsTitle,来自dt的NewsType = 0,NewsType = 0” - 0代表标题?
6 个解决方案
#1
Absolutely. You can use LINQ as Dave Cluderay mentioned. To retrieve your headlines, for example, you could run:
绝对。你可以使用LINQ作为Dave Cluderay提到的。例如,要检索标题,您可以运行:
var myDataTable = dt.AsEnumerable();
var headlines = myDataTable.Where(t => t.NewsID == 0).Take(5);
#3
If you aren't in .NET 3.5, you can create a DataView based on the DataTable object and then set the RowFilter property on the DataView. For example:
如果您不在.NET 3.5中,则可以基于DataTable对象创建DataView,然后在DataView上设置RowFilter属性。例如:
DataView myDV = new DataView(dt);
myDV.RowFilter = "NewsType = 0";
You can then catch only the first 5 rows in your DataView.
然后,您只能捕获DataView中的前5行。
#4
You can use the default view to filter the datatable like so:
您可以使用默认视图来过滤数据表,如下所示:
dt.DefaultView.RowFilter = "NewsType = 0";
dt.DefaultView.RowFilter =“NewsType = 0”;
Not sure how you would get the top 5!?
不确定如何获得前5名!?
#5
If you're not in 3.5 you could use a simple for loop to get the top 5 rows after sorting the table.
如果你不在3.5中,你可以使用一个简单的for循环来排序表后排名前5行。
#6
There is a Select method on the Datatable, dont think there is a way to limit the amount returned. I like the LINQ way, but just an alternative....but the limit might count this out.
数据表上有一个Select方法,不要认为有办法限制返回的数量。我喜欢LINQ方式,但只是一个替代....但限制可能会计算出来。
dt.Select("NewsType = 0");
#1
Absolutely. You can use LINQ as Dave Cluderay mentioned. To retrieve your headlines, for example, you could run:
绝对。你可以使用LINQ作为Dave Cluderay提到的。例如,要检索标题,您可以运行:
var myDataTable = dt.AsEnumerable();
var headlines = myDataTable.Where(t => t.NewsID == 0).Take(5);
#2
You can use LINQ to DataSet if you're in .NET 3.5.
如果您使用的是.NET 3.5,则可以使用LINQ to DataSet。
#3
If you aren't in .NET 3.5, you can create a DataView based on the DataTable object and then set the RowFilter property on the DataView. For example:
如果您不在.NET 3.5中,则可以基于DataTable对象创建DataView,然后在DataView上设置RowFilter属性。例如:
DataView myDV = new DataView(dt);
myDV.RowFilter = "NewsType = 0";
You can then catch only the first 5 rows in your DataView.
然后,您只能捕获DataView中的前5行。
#4
You can use the default view to filter the datatable like so:
您可以使用默认视图来过滤数据表,如下所示:
dt.DefaultView.RowFilter = "NewsType = 0";
dt.DefaultView.RowFilter =“NewsType = 0”;
Not sure how you would get the top 5!?
不确定如何获得前5名!?
#5
If you're not in 3.5 you could use a simple for loop to get the top 5 rows after sorting the table.
如果你不在3.5中,你可以使用一个简单的for循环来排序表后排名前5行。
#6
There is a Select method on the Datatable, dont think there is a way to limit the amount returned. I like the LINQ way, but just an alternative....but the limit might count this out.
数据表上有一个Select方法,不要认为有办法限制返回的数量。我喜欢LINQ方式,但只是一个替代....但限制可能会计算出来。
dt.Select("NewsType = 0");