我有一个课程如下:
Class Scan
Delivered As Boolean
ScanDate As Date
State As String
Facility As String
End Class
然后我创建一个列表并用包含任何内容的扫描填充它。
Dim Scans As New List(Of Scan)
我需要挖掘清单以获取各种信息。我想用LINQ来做。问题是,对于我的生活,我只是没有得到它。语法让我失望,结果没有强类型的事实让我失望,网上的样本过于简单或过于复杂。
我怎么可能
然后我想在For Each循环中使用它。
For Each result In GroupedResults
‘My Code
Next
理想情况下,我希望结果是强类型的。这可能吗?
有人可以推荐一些链接来开始这个吗?我遇到的每个网站都让我头脑发热。我根本不理解它。
修改
非常感谢你们。我仍然对这些东西感到头疼,但至少这是一个真实世界的例子我可以用它来了解发生了什么。
我希望这个简单的例子可以帮助我进入一个更复杂的用途 - 没有运气。我应该这样做了。
所有示例似乎都使用键/值响应。如果我需要分组两个值怎么办?
Class Scan
Delivered As Boolean
Scanned As Boolean
ScanDate As Date
State As String
Facility As String
End Class
1. Get a count of Delivered = True, a count of Scanned=True, grouped by Date
2. Get a count of Delivered = True, a count of Scanned=True, grouped by Facility
3. Get a count of Delivered = True, a count of Scanned=True, grouped by State
是否有可能在一个结果中得到这个?
修改编辑:
回答了我自己的编辑!这似乎对我有用:
Dim query = From r In scans _
Group r By r.ScanDate Into g = Group _
Select New With _
{g, .DeliveredCount = g.Count(Function(s) s.Delivered), .ScannedCount = g.Count(Function(s) s.Scanned)}
非常感谢你们。你让我到了可以解决问题的地步。我仍然没有“得到”我正在做的事情(什么是函数?!),但我有一些东西可以开始。我打算花时间学习这个。我认为真正让我失望的是网上的样本是C#。 Normaly我没有转换语法的问题,但是使用LINQ并不是那么简单。我以为我做错了什么,但只是语法非常不同。
答案 0 :(得分:4)
您不限于使用LINQ的查询语法。您还可以在集合上使用LINQ扩展方法。
您将收到强烈输入的结果。虽然在不返回现有类型时将使用匿名类型。
下面的代码是C#,但你会理解:
static void Main( string[] args )
{
List<Scan> scans = new List<Scan> ();
scans.Add (new Scan (new DateTime (2010, 1, 1), true, "Facility1"));
scans.Add (new Scan (new DateTime (2010, 1, 1), true, "Facility2"));
scans.Add (new Scan (new DateTime (2010, 1, 1), false, "Facility3"));
scans.Add (new Scan (new DateTime (2010, 1, 26), true, "Facility1"));
var result1 = scans.Where (s => s.Delivered).GroupBy (s => s.ScanDate);
foreach( var r in result1 )
{
Console.WriteLine (String.Format ("Number of delivered scans for {0}: {1}", r.Key, r.Count ()));
}
var result2 = scans.Where (s => s.Delivered).GroupBy (s => s.Facility);
foreach( var r in result2 )
{
Console.WriteLine (String.Format ("Number of delivered scans for {0}: {1}", r.Key, r.Count ()));
}
Console.ReadLine ();
}
此处的结果并非真正类型化,因为GroupBy表达式返回IGrouping类型。 但是,你可以这样做:
var result2 = scans.Where (s => s.Delivered)
.GroupBy (s => s.Facility)
.Select( s => new { Facility = s.Key, NumberOfItems = s.Count() } );
使用LINQ提供的扩展方法,可能会帮助您更多地理解它。
对不起C#语法,但我对VB.NET并不熟悉。
答案 1 :(得分:2)
Dim GroupedResults = from s in Scans _
where Delivered _
group s by ScanDate into g //edit here for the others _
select new { Category = g.Key, ScanCount = g.Count() };
如果c#到vb的转换错误,道歉!
答案 2 :(得分:1)
我遇到了同样的困惑。我同意文档不是很清楚。我现在在三个项目中使用LINQ,我更喜欢它。
如果您正在使用LINQ to SQL,请通过在Server View中创建与服务器的连接并将表拖到数据上下文中来创建LINQ类。否则,只需将db设置为扫描对象的集合。
1)获取扫描计数,按日期分配,其中Delivered = True
Dim db = new BrettsDataContext Dim Query = From s as Scan in db _ Where s.Delivered = True _ Order By s.Date
2)获取扫描计数,按设施分组,其中Delivered = True
Dim db = new BrettsDataContext Dim Query2 = From s as Scan in db _ Where s.Delivered = True _ Order By s.Date
3)获取扫描计数,按状态分组,其中Delivered = True
Dim db = new BrettsDataContext Dim Query3 = From s as Scan in db _ Where s.Delivered = True _ Order By s.State
最后一个将获得所有结果行。如果您只想将计数作为数字:
Dim count = (From s as Scan in db _ Where s.Delivered = True).Count
如果您想要所有单独的交货日期,您可以执行以下查询:
Dim db = new BrettsDataContext Dim Query4 = From s as Scan in db _ Where s.Delivered = True _ Order By DeliveryDate
要设置为数据源,只需执行以下操作:
Dim db = new BrettsDataContext Dim Query = From s as Scan in db _ Where s.Delivered = True _ Order By s.Date gridview1.DataSource = Query
希望这有帮助!
答案 3 :(得分:0)
如果您想使用LINQ,我建议您阅读本页使用LINQ with Objects(例如您自己的类Scan):
http://msdn.microsoft.com/en-us/library/bb397937.aspx
应该直截了当地理解而不会让你的头旋转。 :)
否则,我建议使用List类本身,它有许多有用的功能。点击此处查看相关信息: