Reputation: 183
in my program, I use 4 stored procedures, depending on fields that were selected in the form.
ctx = new DataClassesDataContext();
items = (from prjs in ctx.sp_Select_Stuknummers(prjnr) select prjs).ToList();
or
ctx = new DataClassesDataContext();
items = (from prjs in ctx.sp_Select_Stuknummers_Scanner(prjnr, scanner) select prjs).ToList();
and so on ...
I use LINQ to SQL, and for each of these 4 queries, I have a different result class:
sp_Select_StuknummersResult
sp_Select_Stuknummers_ScannerResult
sp_Select_Stuknummers_Scanner_WPSResult
sp_Select_Stuknummers_StuknummerResult
they all have the same fields and definitions.
Now, when I iterate the result:
foreach (sp_Select_StuknummersResult x in items)
{
WPSitems ding = new WPSitems();
ding.ID = x.ID;
ding.Naam = x.Naam;
......
}
I need to pass the type to use (in this example: sp_Select_StuknummersResult )
Is there a way to either A. easily convert to a new type, so that one can be used every time or B. dynamically set the type in the foreach loop ?
maybe there is even a C that I am not aware of... Any help is highly appreciated !
Upvotes: 0
Views: 134
Reputation: 26792
By default, L2S auto-generates a separate type for each individual stored procedure. However, you can easily change that return type in the Linq-2-Sql Designer.
In the designer, click on the stored procedure. In the Properties window, there's an entry 'Return Type': change it from 'Auto-generated type' to the type you want.
If the stored procedure returns rows from an already mapped table, select that type in the drop-down. If not, you can manually add a class to the designer and configure that type to be returned from the stored procedure.
Upvotes: 1
Reputation: 2111
If I understand your problem correctly this might help you out:
1.) Create an interface for the result classes (e.g.) I_SP
:
sp_Select_StuknummersResult
sp_Select_Stuknummers_ScannerResult
sp_Select_Stuknummers_Scanner_WPSResult
sp_Select_Stuknummers_StuknummerResult
2.) Make sure your items
is List<I_SP>
3.) Create a generic method for your foreach
loop
public void MyMethod<T>(List<I_SP> items) where T:I_SP {
foreach (var x in items.OfType<T>)
{
WPSitems ding = new WPSitems();
ding.ID = x.ID;
ding.Naam = x.Naam;
......
}
}
4.) Then just call this like this:
MyMethod<*sp_Select_StuknummersResult result class*>(items);
Hope it's clear enough.
Upvotes: 0
Reputation: 1
You can do this using generics/reflection.
public static WPSitems MapObjectWithIdenticalProperties<T>(T itemOfUnknownType) where T : new()
{
var inputType = typeof(T);
var outputType = typeof(WPSitems);
var outputItem = new WPSitems();
foreach (var inputProperty in inputType.GetProperties())
{
var matchingOutputProperty = outputType.GetProperties().FirstOrDefault(x => x.Name == inputProperty.Name);
if(matchingOutputProperty != null)
matchingOutputProperty.SetValue(outputItem, inputProperty.GetValue(itemOfUnknownType));
}
return outputItem;
}
The function above can be called like this:
var items = GetYourDataThatCanBeDifferentTypes();
var mappedItems = new List<WPSitems>();
foreach(var item in items)
mappedItems.add(MapObjectWithIdenticalProperties(item));
Upvotes: 0