?
Solved

LINQ on stored proc?

Posted on 2015-02-05
15
Medium Priority
?
78 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:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 63

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:Tom Knowlton
ID: 40592089
Correct.

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

what I meant to say was:

"LINQ on DataSet?"
0
 
LVL 63

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Tom 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 63

Accepted Solution

by:
Fernando Soto earned 2000 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:Tom 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:Tom Knowlton
ID: 40592371
Excellent work!
0
 
LVL 63

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:Tom Knowlton
ID: 40592422
Great!  Thanks!
0
 
LVL 63

Expert Comment

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

Author Comment

by:Tom 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:Tom 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 63

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:Tom Knowlton
ID: 40592450
Okay...yep now it works.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40592455
Sorry.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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