How Can I get return values returned by stored procedure using InsertOnSubmit in LINQ - linq

Can I get return values returned by stored procedure using InsertOnSubmit in LINQ.
The stored procedure return a error number. How can I get this error number InsertOnSubmit (using stored procedure instead of Runtime sql). Many Thanks

On the data-context are a number of partial methods for insert/update/delete of each entity type; if you implement the other half of this, you can provide your own SP logic:
partial class MyDataContext {
partial coid UpdateMyEntity(MyEntity instance) {
// your ADO.NET code and/or ExecuteCommand here...
}
}
Note that if you use a separate connection you won't have the same transaction etc (unless it uses TransactionScope).

I overloaded the default function generated by LINQ designer and followed the below steps.
Created a public readonly property _ReturnCode
In the overloaded function I used _ReturnCode = CType(result.ReturnValue, Integer)
Public Function FUNC_NAME( ByRef .....
Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo), ......)
ID= CType(result.GetParameterValue(0), System.Nullable(Of System.Guid))
_ReturnCode = CType(result.ReturnValue, Integer)
Return CType(result.ReturnValue, Integer)
End Function
Let me know if anybody has a better answer. Many Thanks

Related

How to call stored procedure that returns a JSON using Spring JPA

I have a stored procedure that returns a JSON instead of usual table. How do I call it using spring JPA? When I called like normal stored procedure it says
SQL Error: 0, SQLState: S1093
Parameter out was not defined for stored procedure
the way I called the stored procedure is
public interface ITransactionserviceJPA extends JpaRepository<CategoryEntity, Integer>{
#Procedure(procedureName = "getApprovalRequests")
String getApprovalRequests(String EmpId);
}
I checked the stored procedure. It did not have any out parameter. But it returns JSON object so I cannot use use void instead of String. But when I use String as return type it gives error. I searched for few hours but I couldn't find any solution. Idk even this is possible or not.

FUNCTION in WHERE clause of SQL query

I have the following (private) PL/SQL function (inside PACKAGE) returning the previous month in 'YYYYMM' format:
FUNCTION GET_PREV_MONTH RETURN VARCHAR2 IS
BEGIN
RETURN TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYYMM');
END GET_PREV_MONTH;
I need to compare if records are from previous month in several my queries so I wanted to make private function returning previous month. Why I cannot use this return value from GET_PREV_MONTH() function in WHERE clause of my other SQL queries like:
UPDATE mytable t SET t.col=1
WHERE TO_CHAR(t.created,'YYYYMM')=GET_PREV_MONTH();
Oracle says PLS_00231: function 'GET_PREV_MONTH' may not be used in SQL.
If I create this, this works!
UPDATE mytable t SET t.col=1
WHERE TO_CHAR(t.created,'YYYYMM')=TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYYMM');
I wanted to make private function returning previous month. Why I cannot use this return value from GET_PREV_MONTH() function in WHERE clause of my other SQL queries
It is a private function - you cannot reference it outside the package.
If you want to use it in SQL then declare it in the package specification so that it is public.
This is private function of this package! This function is used in procedures from the same package. It is not referenced outside this package.
It CANNOT be used in the SQL context if it is not a public function; regardless of whether the SQL is being called from inside or outside the same package.

Changing values of an object in a LINQ-statement

I want to add some calculated properties to an EntityObject without loosing the possibility of querying it agains the database.
I created a partial class and added the fields I need in the object. Than I wrote a static function "AttachProperties" that should somehow add some calculated values. I cannot do this on clientside, since several other functions attach some filter-conditions to the query.
The functions should look like this:
return query.Select(o =>
{
o.HasCalculatedProperties = true;
o.Value = 2;
return o;
});
In my case the calculated value depends on several lookups and is not just a simple "2". This sample works with an IEnumerable but, of course, not with an IQueryable
I first created a new class with the EntityObject as property and added the other necessary fields but now I need this extended class to be of the same basetype.
First, in my opinion changing objects in a Select() is a bad idea, because it makes something else happen (state change) than the method name suggests (projection), which is always a recipe for trouble. Linq is rooted in a functional programming (stateless) paradigm, so this kind of usage is just not expected.
But you can extend your class with methods that return a calculation result, like:
partial class EntityObject
{
public int GetValue()
{
return this.MappedProp1 * this.MappedProp2;
}
}
It is a bit hard to tell from your question whether this will work for you. If generating a calculated value involves more than a simple calculation from an object's own properties it may be better to leave your entities alone and create a services that return calculation results from an object graph.
Try something like this:
return from o in collection
select new O()
{
OtherProperty = o.OtherProperty,
HasCalculatedProperties = true,
Value = 2
};
This will create a copy of the original object with the changes you require and avoid all the messiness that come with modifying an entity in a select clause.

How do I return strong type object in Linq?

I have a stored proc that returns a list of users (rows in User table).
var admins = db.aspnet_UsersInRoles_GetUsersInRoles('/', "Admin");
LINQ generated aspnet_User classes for me, so can I somehow map the result to a List of aspnet_User type? Something like:
List<aspnet_User> admins = db.aspnet_UsersInRoles_GetUsersInRoles('/', "Admin");
Here is a capture of what is returned.
It's entirely possible that you just need:
List<aspnet_User> admins = db.aspnet_UsersInRoles_GetUsersInRoles('/', "Admin")
.ToList();
But it's hard to know without seeing what type the method call returns.
Perhaps this should be a comment but it is way too long...
Well, you do not really want the internal class <aspnet_User> you should want a MembershipUser.
So how about not using the stored procedure that comes with the membership provider but really use the Membership provider itsself.
There is a beautiful class: Roles in System.Web.Security
And it gives you this:
public static string[] GetUsersInRole(string roleName)
From here, a foreach to get the MembershipUser(s) in a list is not that complicated.
By default a stored procedure will return a type that it determines based on the output columns with Result tacked on to the end. It doesn't associate it with types you have already determined. To change this, you can either change the Return Type in the property window to the type you have already defined in your model, or when dragging the stored proc into your model, drop it directly on the type that you want the stored proc to be mapped into.
You don't get the opportunity to change the column mappings for stored procs however, so make sure the shape that the stored proc generates is the same as your target object structures.
It's an old post and I was working on it today and I get the same issue,
I think you are requesting asp membership ?
You can not convert this stored procedure to aspnet_User because it returns aspnet_UsersInRoles_GetUsersInRolesResult type.
but from this aspnet_UsersInRoles_GetUsersInRolesResult you can get userName, then request the aspnet_User table:
String app = "your application name";
String role = "your role name";
ArrayList userInRoleList = new ArrayList();
//Get the role ID
ASPNETDBDataContext aspDB = new ASPNETDBDataContext();
var userInRole = aspDB.aspnet_UsersInRoles_GetUsersInRoles(app, role);
foreach (aspnet_UsersInRoles_GetUsersInRolesResult users in userInRole)
{
userInRoleList.Add(users.UserName);
}

LINQ-to-SQL select filter

Is there a way to ensure a particular conditional clause is added to the expression tree on each select from a particular table?
For example, a table with a field with the date a record was deleted should never come back or be included in any kind of statement.
Rather than include a where clause each time, is there a way, without creating a view, to add a conditional to each select?
--- Edit for clarity below ---
I'm looking for a function I can partial, much like the Insert/Update/Delete functions, but for Selecting. I want to apply a blanket filter to all queries against a table.
Furthermore, if I get a collection of items from a parent, I want that set to be filtered as well.
Something like:
Private Function BaseItems() As IQueryable(Of Item)
Return (From mi In dataContext.Items Where mi.DeletedAt Is Nothing Select mi)
End Function
Public Function GetItems() as list(of Item)
Return (From mi in BaseItems() select mi).ToList()
End Function
works for functions I write and call. ITEMS, can be a child of MASTER, for example.
'assume TheMaster is a LinqToSQL data class which has a one to many child of Items
TheMaster.Items.Count '<-- will bring back all Items.
How do I always filter what populates by data classes?
You should be able to do this my putting the items into a list and then use lambda expressions to filter the list?
MyListObject.Where(x => x == x.Date);
I'm not sure I understand the context of your question. However you can add more where conditionals on a linq query. If you return an IQueryable, you'll only return a runnable query and you can chain on it other LINQ queries. Afaik it won't execute until you start making it to an IEnumerable or List to iterate through.
Example on LINQ-to-SQL:
MyDataContext context = new MyDataContext();
public IQueryable<MyTable> GetTable() {
return from record in context.Records
where record.Date > DateTime.Now
select record;
}
public IEnumerable<MyTable> GetTableWithinWeek() {
return from record in GetTable()
where record.Date < DateTime.Now.AddDays(7);
select record;
}
I hope my answer makes sense.
AssociateWith in the DataLoadOptions of the DataContext seems to do what I'm needing.

Resources