locked
C# LINQ one condition, return multiple columns and rows RRS feed

  • Question

  • So I'm trying to query a SQL database table using LINQ (I'm actually also using Entity-Framework to
    manage the database: insert, update, delete, etc).

    But for this particular function I'm trying to work on, I just need to query the database.

    So I have a table called TrackingNumbers, as it's defined like this:

    TrackingInfo (    
     Id    
     GUID    
     SeqNum    
     TrackingNumber
    )

    (NOTE: Id in this case is used to prevent errors with E-F and is not really used in queries and thus is a PK, which allows GUID to appear multiple times in the table.)

    (NOTE: GUID is stored as a string, not an SQL or C# GUID and can be repeated in the table.  SeqNum is used for ordering results when they are put into a List)

    Anyway, I'm needing to search by the GUID and return any rows that match the GUID, and specifically, get the SeqNum, Id, and TrackingNumber.  In some cases, multiple rows may need to be returned, depending on how many times the GUID appears in the table (how many rows).

    I've tried the following, and it does execute, but I'm trying to figure out how to extract the data from the result.

    var z = db1.TrackingInfo.Where(x => x.GUID == GUID)
    .Select(x => new TrackingInfo 
    {
     Id = x.Id,
     GUID = x.GUID,
     TrackingNumber = x.TrackingNumber,
     SeqNum = x.SeqNum
    });



    This statement executes, but how can I get a list of rows returned, and be able to extract information from this result?
    (In this particular design, the GUID can appear multiple times in the table, as the Id is the primary key.)

       
    Wednesday, April 4, 2018 5:54 PM

All replies

  • Did you try

    var z = db1.TrackingInfo.Where(x => x.GUID == GUID)
    .Select(x => new TrackingInfo 
    {
     Id = x.Id,
     GUID = x.GUID,
     TrackingNumber = x.TrackingNumber,
     SeqNum = x.SeqNum
    }).ToList();
    Then iterate the results in a foreach?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, April 4, 2018 6:20 PM