denisjoconnor
denisjoconnor

Reputation: 115

2sxc | SQL Datasource - LINQ Filter Query

I have a SQL Datasource setup to get all documents of a certain extension from the standard DNN 'Files' table but I want to add an extra level of specification on what category of file to display but not sure how best to go about it. See my current SQL Datasource code below:

@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";
    }
}

I want to sync the 2sxc Categories entity with DNN's Tab/Page Taxonomy Tags/Categories so as to allow a user to select a DNN Tag on Page setup which (if synced with the 2sxc Categories entity) will allow me to assign a specific doc/excel/pdf file (already connected via 2sxc iCache to a 2sxc Category) to an app based on the SQL Datasource which connects via joining the taxonomy_terms table with the content items table and in turn with the content item tags table which connects with the DNN tabs table.

How can I correct my LINQ/Razor code below to filter my Categories to only display files with the exact 'Services' Category assigned to them. I will use this filter to sync with the Taxonomy Tag 'Services' (exact match) which I want to link to the 2sxc Category (which has an uploaded Adam file already connected via 2sxc iCache) with DNN Taxonomy term '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>
        }

I have looked in detail at the wiki notes on https://github.com/2sic/2sxc/wiki/DotNet-Query-Linq and I am stuck on getting the correct syntax for the category filter with using a foreach with the SQL Datasource template.

Cheers...

Upvotes: 0

Views: 207

Answers (2)

denisjoconnor
denisjoconnor

Reputation: 115

Yes, the filter I needed was as you provided below:

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

Thanks again!

Upvotes: 0

iJungleBoy
iJungleBoy

Reputation: 5638

I believe we have solved this by mail already.

One minor recommendation: if you use DnnSqlDataSource instead of the SqlDataSource you already have the correct connection string for your current DNN. See also http://2sxc.org/en/docs/Feature/feature/4670 as well as https://github.com/2sic/2sxc/wiki/DotNet-DataSources-All

Upvotes: 1

Related Questions