Solved

LINQ on stored proc?

Posted on 2015-02-05
15
75 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 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: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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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

Expert Comment

by:Fernando Soto
ID: 40592455
Sorry.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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