gpt4 book ai didi

c# - 界面投影

转载 作者:可可西里 更新时间:2023-11-01 10:43:01 24 4
gpt4 key购买 nike

我想知道使用 C# 驱动程序 2.0 执行服务器端投影的最佳方法是什么。让我们考虑这种类型

interface IFoo
{
public string Id { get; set; }
public string Name { get; set; }
}


class Foo : IFoo
{
public string Id { get; set; }
public string Name { get; set; }
public string MoreData { get; set; }
}

如何查找所有对象并只返回IdName?这是否执行服务器端投影?

 IMongoCollection<IFoo> collection = /*...*/
return await collection.Find(f => true).ToListAsync<IFoo>();

如果没有,我应该如何为 IFoo 创建一个 ProjectionDefinition

最佳答案

Find 方法返回一个 IFindFluent 对象,您可以使用投影生成器 IFindFluent.Project 方法与 Builders.Projection.Exclude 排除 MoreData 属性的方法:

var q = collection.Find(f => true)
.Project(Builders<Foo>.Projection.Exclude(x => x.MoreData));
var results = await q.ToListAsync();

关于c# - 界面投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31112648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com