I just don't understand how to use Linq

I have a class as follows:

Class Scan
    Delivered As Boolean
    ScanDate As Date
    State As String
    Facility As String  
End Class

Then I create a list and populate it with views containing everything.

Dim Scans As New List(Of Scan) 

I need to open a list to get various pieces of information. I would like to use LINQ for this. The problem is that for life I just do not understand. The syntax throws me off, the fact that the results are not very typed, throws me off, and the sample on the Internet is simplified or too complicated.

How could I

  • Get a counter grouped by date, where Delivered = True
  • Get a counter grouped by Facility, where Delivered = True
  • Get the number of scans grouped by state, where Delivered = True

Then I want to use this in a For Each loop.

For Each result In GroupedResults
    ‘My Code

Next

Ideally, Id, as a result, should be strongly typed. Is it possible?

, ? -, , . .

EDIT:

. , , , , , , .

, spring - . .

, , /. , , ?

    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 

?

Edit:

! , :

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)}

. , . "" , ( ?), - . . , , , - #. , LINQ . , - , , .

+5
4

LINQ. LINQ .

, , . , .

#, :

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, , .

#, VB.NET.

+4
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() };

, # vb !

+2

​​ . , . LINQ , .

LINQ to SQL, LINQ, Server View . 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) , Facility, Delivered = True

Dim db = new BrettsDataContext
Dim Query2 = From s as Scan in db _
Where s.Delivered = True _
Order By s.Date

3) , State, 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

, !

+1

If you want to use LINQ, I would suggest reading this page when using LINQ with objects (for example, your own Scan class):

http://msdn.microsoft.com/en-us/library/bb397937.aspx

It should be simple enough to understand without making your head spin. :)

Otherwise, I would suggest using the List class itself, which has many useful functions. Check here for information about this:

http://msdn.microsoft.com/en-us/library/d9hw1as6.aspx

0
source

All Articles