Solved

LINQ on stored proc?

Posted on 2015-02-05
15
77 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
Technology Partners: 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 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: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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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