Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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/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
.net 使用LINQ选择嵌入式对象(来自MongoDB)_.net_Linq_Mongodb_Norm - Fatal编程技术网

.net 使用LINQ选择嵌入式对象(来自MongoDB)

.net 使用LINQ选择嵌入式对象(来自MongoDB),.net,linq,mongodb,norm,.net,Linq,Mongodb,Norm,我觉得这必须是简单的做,但不能做到这一点-任何帮助将不胜感激。我对LINQ和MongoDB比较陌生 我有以下两个实体: class ParentObject { GUID ParentId {get;set;} IList<ChildObject> ChildObjects {get;set;} ...other properties ParentObject() { childObjects = new List<Ch

我觉得这必须是简单的做,但不能做到这一点-任何帮助将不胜感激。我对LINQ和MongoDB比较陌生

我有以下两个实体:

class ParentObject
{
    GUID ParentId {get;set;}
    IList<ChildObject> ChildObjects {get;set;}
    ...other properties

    ParentObject()
    {
        childObjects = new List<ChildObject>();
    }
}

class ChildObject
{
    GUID ChildId {get;set;}
    ...other properties
}
类父对象
{
GUID ParentId{get;set;}
IList子对象{get;set;}
……其他财产
父对象()
{
childObjects=新列表();
}
}
类子对象
{
GUID ChildId{get;set;}
……其他财产
}
我坚持使用MongoDB实例(作为一个文档,嵌入子文档)。我需要做的是使用ParentId和ChildId仅检索ParentObject文档中存储的ChildObject子文档之一。我有下面的方法/方法调用,几乎实现了我想要的功能,但我只能基于索引(o、1等)返回一个IList或一个特定的ChildObject:

//我知道这行不通
var parentId=(某些GUID);
var childId=(某些GUID);
var结果=
单选
(
x=>x.Id==parentId&&x.ChildObjects.ChildId==ChildId,
y=>y.ChildObject
);
公共TResult单选(
System.Linq.Expressions.Expression whereExpression,
System.Linq.Expressions.Expression选择表达式)
其中T:class,new()
{
TResult retval=默认值(TResult);
使用(var db=Mongo.Create(ConnectionString()))
{
retval=db.GetCollection().AsQueryable()
.Where(whereExpression)
.选择(选择表达式)
.SingleOrDefault();
}
返回返回;
}
非常感谢您的帮助/指点


-Mike

目前Mongodb本身不支持此功能。在匹配时,将返回整个文档,而不仅仅是匹配的子文档。我不确定norm是否为这一点提供了支持,并在驾驶员侧提供了过滤器。mongodb上此功能请求的打开JIRA项是使用map/reduce检索嵌入文档。

只需两个步骤:1。按父ID加载父项。2.从父级按子级id获取子级:parent.ChildCollection.Single(x=>x.id==childId);这是可行的,在服务器端这样做会更好,但由于它不受支持,这就行了-谢谢。
// I know this won't work    

var parentId = (Some GUID);
var childId = (Some GUID);

var result = 
    SingleWithSelect<ParentObject, ChildObject>
    (
        x => x.Id == parentId && x.ChildObjects.ChildId == ChildId, 
        y => y.ChildObject  
    );


public TResult SingleWithSelect<T, TResult>(
    System.Linq.Expressions.Expression<Func<T, bool>> whereExpression,
    System.Linq.Expressions.Expression<Func<T, TResult>> selectExpression)
    where T : class, new()
        {
            TResult retval = default(TResult);
            using (var db = Mongo.Create(ConnectionString()))
            {
                retval = db.GetCollection<T>().AsQueryable()
                            .Where(whereExpression)
                            .Select(selectExpression)
                            .SingleOrDefault();
            }
            return retval;

        }