Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/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# foreach中的C Linq子查询_C#_Linq_Sqlite - Fatal编程技术网

C# foreach中的C Linq子查询

C# foreach中的C Linq子查询,c#,linq,sqlite,C#,Linq,Sqlite,我有一个Linq查询Foo有一个DateTime变量: var tableFoo = context.GetTable<Foo>(); var tableBar = context.GetTable<Bar>(); var preselect = tableFoo.Where(o => o.Name == ""); List<Foo> foos = new List<Foo>(); foreach (Foo f in preselect

我有一个Linq查询Foo有一个DateTime变量:

var tableFoo = context.GetTable<Foo>();
var tableBar = context.GetTable<Bar>();

var preselect = tableFoo.Where(o => o.Name == "");

List<Foo> foos = new List<Foo>();

foreach (Foo f in preselect) //System.InvalidCastException comes here at the second iteration (Unable to cast to DateTime)
{
    Foo foo = f;

    var subselect = tableBar.Where(o => o.Id == foo.Id);
    foreach (Bar bar in subselect)
    {
        foo.bars.Add(bar);
    }
    foos.Add(foo);
}
当预选包含1个对象时,它可以正常工作。 但如果预选包含多个对象,它将无法按预期工作。在第一次迭代中它可以工作,但在第二次迭代中我得到一个System.InvalidCastException。 它与内部子选择有关,因为如果我移除内部foreach,它将完美工作。 我认为这与我的工作有关。但我不知道该改变什么


预选中的子选择不适合我的应用程序架构

我这样解决了它,我知道它不是很优雅:

var tableFoo = context.GetTable<Foo>();
var tableBar = context.GetTable<Bar>();

var preselect = tableFoo.Where(o => o.Name == "");

List<Foo> foos = new List<Foo>();

foreach (Foo f in preselect) //System.InvalidCastException comes here at the second iteration (Unable to cast to DateTime)
{
    Foo foo = f;

    foos.Add(foo);
}

foreach (Foo foo in foos)
{
    var subselect = tableBar.Where(o => o.Id == foo.Id);
    foreach (Bar bar in subselect)
    {
        foo.bars.Add(bar);
    }
}