Tom Knowlton
asked on
LINQ on stored proc?
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:
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();
}
}
}
So you got the data to your local machine. Now how do you want to use Linq to filter the data set?
ASKER
Correct.
I just realized I said "LINQ on stored proc?"
what I meant to say was:
"LINQ on DataSet?"
I just realized I said "LINQ on stored proc?"
what I meant to say was:
"LINQ on DataSet?"
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.
ASKER
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)"
I want to impose a where condition on the data that comes back.
"WHERE sometablealias.LocalNumber
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
((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}
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)" ?
ASKER
Excellent work!
Hi knowlton;
If you are just wanting certain values then you can use this
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);
ASKER
Great! Thanks!
Not a problem knowlton, glad to help.
ASKER
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."
select row);
it is underlining the "row" - says "Only assignment, call, increment, decrement and new expressions can be used as a statement."
ASKER
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);
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;
select row;
ASKER
Okay...yep now it works.
Sorry.