Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

LINQ on stored proc?

I want to bring back a stored procedure, put it into a DataSet and then use LINQ to narrow the results (instead of passing in parameters)

It is the LINQ part I need help with.

Here is the current code:

private void foo(object sender, EventArgs e)
        {
            if (object.Equals(null, DataSource))
            {
                DBConnection db = null;

                try
                {
                   
                    db = DBConnection.CreateConnection("Default", 9);
                    db.SetProcedureName("UNAdmin.unsp_getmesomedata");
                    //db.AddParameter("@bar", _barid, false);
                    DataSet ds = db.ExecuteDataSet();

                    if (!object.Equals(null, ds))
                    {
                        if (ds.Tables.Count > 0)
                        {
                            DataSource = ds;
                            DataMember = ds.Tables[0].TableName;
                        }
                    }
                }
                catch (Exception ex)
                {
                    problem.Log.Error("some problem", ex);
                }
                finally
                {
                    if (!object.Equals(null, db))
                        db.Close();
                    if (!object.Equals(null, ci))
                        ci.Dispose();                
                }
            }
        }

Open in new window

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

So you got the data to your local machine. Now how do you want to use Linq to filter the data set?
Avatar of Tom Knowlton

ASKER

Correct.

I just realized I said "LINQ on stored proc?"

what I meant to say was:

"LINQ on DataSet?"
But that does not answer my question. Now that you have the data set filled when you run a Linq query what do you want returned back from that query? For example I want all rows that have a _barid between 1 and 150.
I'm sorry, you are right.  I did not answer your question.

I want to impose a where condition on the data that comes back.

"WHERE sometablealias.LocalNumber IN (234, 432)"
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
((System.Data.EnumerableRowCollection<System.Data.DataRow>)query).AsDataView()
{System.Data.LinqDataView}
    [System.Data.LinqDataView]: {System.Data.LinqDataView}
    base {System.ComponentModel.MarshalByValueComponent}: {System.Data.LinqDataView}
    AllowDelete: true
    AllowEdit: true
    AllowNew: true
    ApplyDefaultSort: false
    Count: 351
    DataViewManager: null
    IsInitialized: true
    RowFilter: null
    RowStateFilter: Unchanged | Added | ModifiedCurrent
    Sort: ""
    Table: {Table}

Open in new window



Based on a quick check -- I am getting the number of rows back I expected.


What is the LINQ equivalent of a T-SQL   "IN"  type statement ...

"WHERE LocalNumber IN (243, 444, 888, 432)"  ?
Excellent work!
Hi knowlton;

If you are just wanting certain values then you can use this

List<int> localNumbers = new List<int>() { 243, 444, 888, 432 };

IEnumerable<DataRow> results = from row in ds.Tables[0].AsEnumerable()
                               where localNumbers.Contains( row.Field<int>("LocalNumber"))
                               select row);

Open in new window

Great!  Thanks!
Not a problem knowlton, glad to help.
in this part:

select row);


it is underlining the "row" - says "Only assignment, call, increment, decrement and new expressions can be used as a statement."
My current code:

 List<string> localNumbers = new List<string>() { _LocalNumberList };

                    IEnumerable<DataRow> results = from row in ds.Tables[0].AsEnumerable()
                               where localNumbers.Contains( row.Field<string>("LocalNumber"))
                               select row);

Open in new window

Sorry in my last post I have an error. The select clause should be as follows. I had an extra ) at the end.

select row;
Okay...yep now it works.