locked
filter with DataContext RRS feed

  • Question

  • Hi All

    I use  the LINQ

    and I use the DataContext to get the data from the database , but I have the table has large rows and the DataContext gets the all rows

    how filter this data to get one row

    pleas help me

     

     

    Sunday, March 13, 2011 2:39 PM

Answers

  • Hi Ora-dbaabode;
    You will need to post the creation of the data context and all the queries executed to that data context. The query I posted will only return one complete row from the table or no rows at all. The query sent to the server will be very much like the following:

    SELECT TOP (1) [t0].[Id], [t0]..., ..., [t0].nnn
    FROM [dbo].[TableName] AS [t0]
    WHERE [t0].[Id] = @p0
    -- @p0: Input int [1234567]
    -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1
    
    

    As you can see from the select clause only the Top(1) rows will be returned. Some other query is filling the table.

    Fernando


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Ora-dbaabode Monday, March 14, 2011 1:15 PM
    Sunday, March 13, 2011 5:35 PM

All replies

  • Hi Ora-dbaabode;

    Well we need a little more information. On what column of the table do you want to filter on? If after filtering there are more then one row, which row do you want back?

    Fernando


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, March 13, 2011 3:46 PM
  • I have Item Table and it has Id Column  , I need   get one row ,which  has Id is  1234567
    Sunday, March 13, 2011 4:21 PM
  • Hi Ora-dbaabode;
    Something like this should give you the results that you want.

    DataContext db = new DataContext();
    
    var idRow = (from row in db.TableName
           where row.Id == 1234567
           select row).FirstOrDefault();
    
    

    The variable idRow will hold a single row or null if row not found.

    Fernando


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, March 13, 2011 4:56 PM
  • thank you Fernandoo

    but , I have the website and I need this data ,

    when I execute this statment I noted it got  the all rows in table and return one row!

     

    Sunday, March 13, 2011 5:08 PM
  • Hi Ora-dbaabode;
    You will need to post the creation of the data context and all the queries executed to that data context. The query I posted will only return one complete row from the table or no rows at all. The query sent to the server will be very much like the following:

    SELECT TOP (1) [t0].[Id], [t0]..., ..., [t0].nnn
    FROM [dbo].[TableName] AS [t0]
    WHERE [t0].[Id] = @p0
    -- @p0: Input int [1234567]
    -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1
    
    

    As you can see from the select clause only the Top(1) rows will be returned. Some other query is filling the table.

    Fernando


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Ora-dbaabode Monday, March 14, 2011 1:15 PM
    Sunday, March 13, 2011 5:35 PM
  • thank you Fernandoo

     

    Monday, March 14, 2011 1:15 PM
  • Not a problem; glad I was able to help.

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, March 14, 2011 1:31 PM