Solved

LINQ on stored proc?

Posted on 2015-02-05
15
74 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASCX file or a newer alternative? 1 34
Web site error 3 36
Problem to refer to value 8 45
asp.net mvc5 6 11
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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

815 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

8 Experts available now in Live!

Get 1:1 Help Now