Solved

LINQ on stored proc?

Posted on 2015-02-05
15
72 Views
Last Modified: 2015-02-05
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

0
Comment
Question by:knowlton
  • 8
  • 7
15 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40592082
So you got the data to your local machine. Now how do you want to use Linq to filter the data set?
0
 
LVL 5

Author Comment

by:knowlton
ID: 40592089
Correct.

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

what I meant to say was:

"LINQ on DataSet?"
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40592109
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.
0
 
LVL 5

Author Comment

by:knowlton
ID: 40592158
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)"
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40592298
The below query will return all row's in the DataTable matching the where clause. Note that you must enumerate the query variable results to return the collection of DataRow. if you wish to execute the query immediately change IEnumerable to List and surrounding the complete query with parentheses and adding ".ToList()" after the closing parentheses.

int start = 234;
int end = 432;

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

// Please note in the code where you see this, row.Field<int>("LocalNumber"), change <int> to the correct data type of the column and change this, LocalNumber, to the correct column name if different.

Open in new window

0
 
LVL 5

Author Comment

by:knowlton
ID: 40592370
((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)"  ?
0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 40592371
Excellent work!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40592413
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

0
 
LVL 5

Author Comment

by:knowlton
ID: 40592422
Great!  Thanks!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40592427
Not a problem knowlton, glad to help.
0
 
LVL 5

Author Comment

by:knowlton
ID: 40592435
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."
0
 
LVL 5

Author Comment

by:knowlton
ID: 40592444
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

0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40592447
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;
0
 
LVL 5

Author Comment

by:knowlton
ID: 40592450
Okay...yep now it works.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40592455
Sorry.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now