2sxc | SQL 数据源 - LINQ 过滤器查询

2sxc | SQL Datasource - LINQ Filter Query

我有一个 SQL 数据源设置,可以从标准 DNN 'Files' table 获取特定扩展的所有文档,但我想在哪个类别上添加额外级别的规范要显示的文件数量,但不确定如何最好地处理它。请参阅下面我当前的 SQL 数据源代码:

@using ToSic.Eav.DataSources
@functions
{
    // Default data initialization - should be the place to write data-retrieval code
    // In the future when routing & pipelines are fully implemented, this will usually be an external configuration-only system
    // For now, it's done in a normal event, which is automatically called by the razor host in 2SexyContent
    public override void CustomizeData()
    {
        var source = CreateSource<SqlDataSource>();
        // source.TitleField = "EntityTitle"; // not necessary, default
        // source.EntityIdField = "EntityId"; // not necessary, default
        // source.ConnectionString = "...";   // not necessary, we're using the ConnectionStringName on the next line
        source.ConnectionStringName = Content.ConnectionName;

    // Special note: I'm not selecting * from the DB, because I'm activating JSON and want to be sure that no secret data goes out
    source.SelectCommand = "Select Top 10 FileId as EntityId, FileName as EntityTitle, PublishedVersion, UniqueId, FileName, Size as Size, Extension as Extension, CreatedOnDate as Date, Folder as Folder FROM Files WHERE PortalId = @PortalId AND Extension = 'docx' OR Extension = 'xlsx' OR Extension = 'pdf'";
    source.Configuration.Add("@PortalId", Dnn.Portal.PortalId.ToString());
    Data.In.Add("FileList", source.Out["Default"]);

    // enable publishing
    Data.Publish.Enabled = true;
    Data.Publish.Streams = "Default,FileList";
    }
}

我想将 2sxc 类别实体与 DNN 的 Tab/Page 分类法 Tags/Categories 同步,以便允许用户 select 页面设置上的 DNN 标签(如果与2sxc 类别实体)将允许我将特定的 doc/excel/pdf 文件(已经通过 2sxc iCache 连接到 2sxc 类别)分配给基于 SQL 数据源的应用程序,该数据源通过加入 taxonomy_terms 连接table 与内容项 table 和内容项标签 table 连接到 DNN 选项卡 table.

我如何更正下面的 LINQ/Razor 代码以过滤我的类别以仅显示分配给它们的确切 'Services' 类别的文件。我将使用此过滤器与我想要 link 的分类标签 'Services'(完全匹配)与 DNN 分类术语同步到 2sxc 类别(其中上传的 Adam 文件已通过 2sxc iCache 连接) 'Services'?

@foreach (var file in AsDynamic(Data.In["FileList"]).Where(i => 
        (i.Category as List<dynamic>).Any(c => c.EntityId == FileList.EntityId)))
        {
            <li>@file.FileName</li>
        }

我详细查看了 https://github.com/2sic/2sxc/wiki/DotNet-Query-Linq 上的 wiki 注释,我一直坚持使用带有 SQL 数据源模板的 foreach 来获取类别过滤器的正确语法。

干杯...

我相信我们已经通过邮件解决了这个问题。

一个小建议:如果您使用 DnnSqlDataSource 而不是 SqlDataSource,则您已经拥有当前 DNN 的正确连接字符串。另见 http://2sxc.org/en/docs/Feature/feature/4670 as well as https://github.com/2sic/2sxc/wiki/DotNet-DataSources-All

是的,我需要的过滤器如下所示:

@using ToSic.SexyContent

    @{
    // all QandA items
    var all = AsDynamic(App.Data["QandA"].List);

    // the filter value, can be set in template
    // but usually you would either get it from url with Request["urlparam"]
    // or from a setting in the view, using Content.Category
    var currentCat = "Business";

    // filter, find any which have the current category
    var filtered = all

    .Where(p => (p.Categories as List<DynamicEntity>).Any(c => AsDynamic(c).Name == currentCat)); 

}

<div class="clearfix">
        @foreach (var q in filtered)
        {
            <div class="sc-element" data-tags="@String.Join(",", ((List<DynamicEntity>)q.Categories).Select(a => AsDynamic(a).EntityId))">

                @Edit.Toolbar(Content)

                <div class="col-md-12">
                    <div class="">
                        <a href="@q.Link" class="">
                            <span class="">@q.Link</span>
                        </a>
                        <p>@q.Title</p>
                    </div>
                </div>
            </div>
        }
</div>

再次感谢!