Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# sqlite.net表,其中子表上的条件_C#_Xamarin.forms_Sqlite Net Extensions_Sqlite.net - Fatal编程技术网

C# sqlite.net表,其中子表上的条件

C# sqlite.net表,其中子表上的条件,c#,xamarin.forms,sqlite-net-extensions,sqlite.net,C#,Xamarin.forms,Sqlite Net Extensions,Sqlite.net,我正在使用Xamarin表单、SQLite.net和SQLitenet扩展,我无法理解为什么我期望简单的东西不起作用 我有两节课 public class MeasurementInstanceModel { public MeasurementInstanceModel () { } [PrimaryKey] [AutoIncrement] public int Id { get; set; }

我正在使用Xamarin表单、SQLite.net和SQLitenet扩展,我无法理解为什么我期望简单的东西不起作用

我有两节课

public class MeasurementInstanceModel
{
    public MeasurementInstanceModel ()
    {
    }

    [PrimaryKey]
    [AutoIncrement]
    public int Id {
        get;
        set;
    }

    [ForeignKey(typeof(MeasurementDefinitionModel))]
    public int MeasurementDefinitionId {
        get;
        set;
    }

    [ManyToOne(CascadeOperations = CascadeOperation.CascadeRead)]
    public MeasurementDefinitionModel Definition {
        get;
        set;
    }

    [ForeignKey(typeof(MeasurementSubjectModel))]
    public int MeasurementSubjectId {
        get;
        set;
    }

    [ManyToOne(CascadeOperations = CascadeOperation.CascadeRead)]
    public MeasurementSubjectModel Subject {
        get;
        set;
    }

    public DateTime DateRecorded {
        get;
        set;
    }

    [OneToMany(CascadeOperations = CascadeOperation.All)]
    public List<MeasurementGroupInstanceModel> MeasurementGroups {
        get;
        set;
    }
}
公共类度量标准模型
{
公共度量标准模型()
{
}
[主密钥]
[自动增量]
公共整数Id{
得到;
设置
}
[外键(类型(度量定义模型))]
公共int度量定义ID{
得到;
设置
}
[MANYTONE(级联操作=级联操作.级联读取)]
公共度量定义模型定义{
得到;
设置
}
[外键(类型(度量对象模型))]
公共int测量对象{
得到;
设置
}
[MANYTONE(级联操作=级联操作.级联读取)]
公共测量对象模型主题{
得到;
设置
}
公共日期时间日期记录{
得到;
设置
}
[OneToMany(级联操作=级联操作.All)]
公共列表度量组{
得到;
设置
}
}

公共类度量对象模型
{
[主密钥]
[自动增量]
公共整数Id{
得到;
设置
}
公共字符串名{
得到;
设置
}
[OneToMany(级联操作=级联操作.All)]
公共列表度量状态{get;set;}
}
我只是尝试执行以下查询,但它总是失败

db.Table<MeasurementInstanceModel>().Where(w => w.Subject.Name == avariable);
db.Table().Where(w=>w.Subject.Name==avariable);
我得到这个例外

System.Diagnostics.Debugger.Mono_UnhandledException(ex={System.Reflection.TargetInvocationException:异常已由调用的目标引发。-->System.NullReferenceException:对象引用未设置为对象的实例 在SQLite.Net.TableQuery
1[MeasureONE.MeasurementInstanceModel].CompileExpr(System.Linq.Expressions.expr,System.Collections.Generic.List
1 queryArgs)[0x00000]中:0 在SQLite.Net.TableQuery
1[MeasureONE.MeasurementInstanceModel].CompileExpr(System.Linq.Expressions.expr,System.Collections.Generic.List
1 queryArgs)[0x00000]中:0 在SQLite.Net.TableQuery
1[MeasureONE.MeasurementInstanceModel].CompileExpr(System.Linq.Expressions.expr,System.Collections.Generic.List
1 queryArgs)[0x00000]中:0 在SQLite.Net.TableQuery
1[MeasureONE.MeasurementInStatanceModel].GenerateCommand(System.String selectionList)[0x00000]中:0
在SQLite.Net.TableQuery
1[MeasureONE.MeasurementInstanceModel].GetEnumerator()[0x00000]中:0 在System.Collections.Generic.List
1[MeasureONE.MeasurementInstanceModel].AddEnumerable(IEnumerable
1 enumerable)[0x00000]中:0 在System.Collections.Generic.List
1[MeasureONE.MeasurementInstanceModel]…中的子对象(IEnumerable
1集合)[0x00000]中:0 在System.Linq.Enumerable.ToList[MeasurementInstanceModel](IEnumerable
1源代码)[0x00000]中:0
在MeasureONE.Repository
1[MeasureONE.MeasurementInstanceModel].GetAll[DateTime](System.Linq.Expressions.Expression
1谓词,System.Linq.Expressions.Expression
1 orderBy,Nullable
1递减,Nullable
1跳过,Nullable
1计数)[0x00094]在/Users/jean-sebastiencate/MeasureONE/MeasureONE/Models/Repository/Repository.cs:48中
在MeasureONE.Repository
1[MeasureONE.MeasurementInstanceModel].GetAllWithChildren[DateTime](System.Linq.Expressions.Expression
1谓词,System.Linq.Expressions.Expression
1 orderBy,Nullable
1递减,Nullable
1跳过,Nullable
1计数)[0x00009]在/Users/jean-sebastiencate/MeasureONE/MeasureONE/Models/Repository/Repository.cs:54中
在/Users/jean-sebastiencate/MeasureONE/MeasureONE/MeasureONE/MeasureONE/ViewModels/MeasureONE/ViewModels/MeasurementListViewModel.cs:42中的MeasureONE.measurementListModel.Load(System.Linq.Expressions.Expression
1 pred,Nullable
1 skip,Nullable
1 count)[0x00049] 在/Users/jean-sebastiencote/MeasureONE/MeasureONE/ViewModels/MeasurementListViewModel.cs中的MeasureONE.measurementlistview.Load(MeasureONE.filtervewmodel过滤器)[0x000cf]处 在/Users/jean-sebastiencate/MeasureONE/MeasureONE/ViewModels/MeasureONE/ViewModels/MeasurementListViewModel.cs:21中的MeasureONE.measurentlistview.m_1(GalaSoft.MvvmLight.Messaging.NotificationMessage`1 msg)[0x00007]处 at(包装器管理为本机)System.Reflection.monmethod:InternalInvoke(System.Reflection.monmethod,object,object[],System.Exception&) 在System.Reflection.MonMethod.Invoke(System.Object obj、BindingFlags invokeAttr、System.Reflection.Binder Binder、System.Object[]参数、System.Globalization.CultureInfo区域性)[0x00000]中:0 ---内部异常堆栈跟踪的结束---


正如您从堆栈跟踪中看到的,代码中还有一些东西,比如order by。但是,只要在子表上没有条件,这些都可以正常工作。

SQLite Net Extensions没有添加任何查询功能(至少到目前为止)。这意味着您无法在查询时访问关系,因为该对象需要一个未执行的联接。这就是您获取
NullReferenceException
的原因

您需要手动执行联接。替换此代码:

db.Table<MeasurementInstanceModel>().Where(w => w.Subject.Name == avariable);
这样,您不必手动键入联接,结果完全相同,但是此选项会受到影响,因此可能会受到一些性能损失


希望有帮助。

这是我最初做的,但它不起作用。我不知道SQLite正在使用双相等而不是单相等。长话短说,它起作用了。令人失望的是,这种情况不受支持。我将看看是否可以根据模型上的属性构建自定义解决方案。我已经担心som就这样,,
db.Table<MeasurementInstanceModel>().Where(w => w.Subject.Name == avariable);
var result = conn.Query<MeasurementInstanceModel>(
    "SELECT * " +
    "FROM MeasurementInstanceModel AS it " +
    "JOIN MeasurementSubjectModel AS sb " +
    "ON it.MeasurementSubjectId == sb.Id " +
    "WHERE sb.Name == ?", avariable);
var subjects = conn.GetAllWithChildren<MeasurementSubjectModel>(s => s.Name == avariable);
var result = subjects.Select(s => s.MeasurementInstances).Distinct().ToList();