【问题标题】:2sxc | SQL Datasource - LINQ Filter Query2sxc | SQL 数据源 - LINQ 过滤器查询
【发布时间】:2017-04-19 13:03:04
【问题描述】:

我有一个 SQL 数据源设置,可以从标准 DNN“文件”表中获取具有特定扩展名的所有文档,但我想添加一个额外级别的规范来说明要显示的文件类别,但不确定如何最好地进行它。请参阅下面我当前的 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 的选项卡/页面分类标签/类别同步,以允许用户在页面设置上选择 DNN 标记(如果与 2sxc 类别实体同步)将允许我分配一个特定的 doc/excel/pdf 文件(已经通过 2sxc iCache 连接到 2sxc 类别)到基于 SQL 数据源的应用程序,该应用程序通过将 taxonomy_terms 表与内容项表连接,然后与内容项标签表连接DNN 选项卡表。

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

@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 来获取类别过滤器的正确语法。

干杯...

【问题讨论】:

    标签: linq razor datasource 2sxc


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      @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>
      

      再次感谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-12
        • 2015-10-18
        • 1970-01-01
        • 1970-01-01
        • 2015-09-15
        • 2010-11-13
        相关资源
        最近更新 更多