Reputation: 35
I'm trying to do a simple Join on 2 tables and return a Value Tuple of those two tables.
public partial class DeliveryMethod
{
[Required]
[PrimaryKey]
public int DeliveryMethodId { get; set; }
[References(typeof(Event))]
[Required]
public string EventId { get; set; }
[References(typeof(DeliveryType))]
[Required]
public short DeliveryTypeId { get; set; }
[Required]
public int MappedValue { get; set; }
}
public partial class DeliveryType
{
[Required]
[PrimaryKey]
public short DeliveryTypeId { get; set; }
[Required]
public string DeliveryTypeDescription { get; set; }
}
public List<(DeliveryMethod deliveryMethod, DeliveryType deliveryType)> GetDeliveries(string eventId)
{
using (var db = DbFactory.OpenDbConnection(...))
{
var q = db.From<DeliveryMethod>()
.Join<DeliveryType>((dm, dt) => dm.DeliveryType == dt.DeliveryType)
.Where(dm => dm.EventId == eventId)
.Select<DeliveryMethod, DeliveryType>((dm, dt) =>
new {dm, dt});
return db.Select<(DeliveryMethod deliveryMethod, DeliveryType deliveryType)>(q);
}
}
However, when I run this, I get a NullReferenceException. This seems to be because ConvertToValueTuple
in OrmLiteUtils
only has converters for basic types like string, int, DateTime, etc. and GetConverter(fieldType) returns null when it's a type of a custom object.
Is there a work around this? Or some way to return a value tuple of more complex, custom objects instead of just basic tuples like (int id, string name, DateTime time)?
P.S. I tried to simplify my problem by simplifying the classes so if I made a mistake there, I apologize for that but I think you can get the basic idea behind my question.
Upvotes: 2
Views: 200
Reputation: 143374
You can only use OrmLite's C# 7 Tuple support by selecting columns, not entire tables, e.g:
.Select<DeliveryMethod, DeliveryType>((dm, dt) =>
new {dm.EventId, dt.DeliveryMethodId});
var results = db.Select<(string, int)>(q);
For Selecting entire tables checkout OrmLite's SelectMulti API, e.g:
var q = db.From<DeliveryMethod>()
.Join<DeliveryType>((dm, dt) => dm.DeliveryType == dt.DeliveryType)
.Where(dm => dm.EventId == eventId);
var results = db.SelectMulti<DeliveryMethod, DeliveryType>();
Upvotes: 1