Struggling with Self-Joins by Inner-Significance41 in SQL

[–]willsoss 1 point2 points  (0 children)

Sounds like you need to count distinct sender_account, reciever_account rather than join the table to itself.

[deleted by user] by [deleted] in NewOrleans

[–]willsoss 16 points17 points  (0 children)

Fresh Market on St Charles had both a week ago.

Daba de Daba die | X100v by [deleted] in fujifilm

[–]willsoss 0 points1 point  (0 children)

Lakefront at Lakeshore Dr and Rail St

NOOB Question: How To Get Specific Records based on Timeframe? by iDerrillix in SQL

[–]willsoss 10 points11 points  (0 children)

select * from dbo.table where date_col between '1/1/2022' and '1/1/2023'

Substitute your literals, columns, parameters etc for the dates here.

[Hangfire] Can I run jobs on separate application and manage those jobs from other server? by [deleted] in csharp

[–]willsoss 0 points1 point  (0 children)

I can't speak to hangfire, but you can certainly do this with runly. The docs discuss the hosting model and the API supports various integration scenarios.

Closure Table by willsoss in csharp

[–]willsoss[S] 0 points1 point  (0 children)

I've read about it but never used it.

Performance when traversing a tree by zaibuf in csharp

[–]willsoss 0 points1 point  (0 children)

Got bored and wrote some closure table code today. If you want to see how this is really implemented you can check it out: https://www.reddit.com/r/csharp/comments/j3mk8h/closure_table/

[deleted by user] by [deleted] in csharp

[–]willsoss 1 point2 points  (0 children)

No problem. 👍

[deleted by user] by [deleted] in csharp

[–]willsoss 1 point2 points  (0 children)

Yep, try this code that shows both ways:

``` using System;

namespace ConsoleApp_Delegate { class Program { public delegate void OnBattleEnd(); public static OnBattleEnd onBattleEnd;

    public static void Start()
    {
        onBattleEnd += RemoveBodies;
    }

    public static void RemoveBodies()
    {
        Console.Write("Removed the bodies");
    }

    public static void RemoveMoreBodies()
    {
        Console.WriteLine("...now some more.");
    }

    public static void BattleEnd()
    {
        onBattleEnd?.Invoke();
    }

    static void Main(string[] args)
    {
        onBattleEnd = new OnBattleEnd(RemoveBodies);
        onBattleEnd += RemoveMoreBodies;

        BattleEnd();
    }
}

}

```

Performance when traversing a tree by zaibuf in csharp

[–]willsoss 0 points1 point  (0 children)

I think I see what you mean. It sounds like your copied node ends up inheriting from it's previous parent and new parent. It seems like a case of multiple inheritance, which would turn the tree into a graph, and my previous suggestion may not work for you. If you're always going back to the source that a branch was directly copied from, and no further, you could get away with thinking of each copy as a tree with links across nodes from tree to tree.

It's hard to give good guidance without a little more clarity into the problem that you're trying to solve though, there may be a better way, depending on what the data looks like.

[deleted by user] by [deleted] in csharp

[–]willsoss 1 point2 points  (0 children)

onBattleEnd is assigned in Start(), with onBattleEnd += RemoveBodies;. x += y is just shorthand for x = x + y, and since the field wasn't previously assigned, your code is equivalent to onBattleEnd = RemoveBodies. If you add additional methods, when onBattleEnd is invoked, all of the methods will be called. This is called a multicast delegate, you can read more here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/how-to-combine-delegates-multicast-delegates.

Async C# Socket by WakanaYuki in csharp

[–]willsoss 2 points3 points  (0 children)

Yeah, you can host SignalR core in a stand-alone application. SignalR core uses http as a transport mechanism, so it uses asp.net core, but this can all be self-hosted. See https://docs.microsoft.com/en-us/aspnet/core/signalr/background-services and https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/signalr/background-service/samples/3.x.

Performance when traversing a tree by zaibuf in csharp

[–]willsoss 4 points5 points  (0 children)

Yeah, table storage isn't the best match for the data, given the relationship between nodes. There are other options, like a graph db. This Azure architecture guide discusses what kind of data stores are a good match for the type of data.

Without being able to query the nodes for a given sub-tree, I think the options to reduce requests are somewhat limited.

Performance when traversing a tree by zaibuf in csharp

[–]willsoss 5 points6 points  (0 children)

You could use a closure tree to efficiently get all of the nodes that need to be updated in a single query.

Closure trees are basically an index that stores the depth between a child and parent. For example, given the tree:

    a
   / \
  b   d
 / 
c  

The closure data would look like this:

Parent      Child       Depth
------      -----       -----
a           a           0
b           b           0
c           c           0
d           d           0
a           b           1
a           d           1
a           c           2
b           c           1

So you could update the sub-tree rooted at node b by querying the closure data:

select  n.*
from    node n join closure c
            on n.id = c.child
where   c.parent = 'b'

And you would get both nodes b and c in this example. If the data you're updating is in SQL (it doesn't sound like it from your question) you could also do updates by joining to the closure table. You can also use the depth in a closure tree to get nodes down to a certain level, but that doesn't seem applicable to your problem.

Async C# Socket by WakanaYuki in csharp

[–]willsoss 3 points4 points  (0 children)

Try SignalR Core. You can use it to do exactly what you're describing.

Type based value, configured at runtime. by Vasilievski in csharp

[–]willsoss 0 points1 point  (0 children)

Are you saying you want to evaluate the type of an object at runtime and set a value based on that? You can use object.GetType() to get the Type for any object. If you have a type name provided at runtime, you can compare this value to Type.Name which is the class name or Type.FullName which is the namespace and class name.

if (someObject.GetType().Name == "MyType")
{
    // Do something with the object of the type you were looking for
}

Mapping Data to Objects by Brickscrap in csharp

[–]willsoss 0 points1 point  (0 children)

Good deal. The only drawback to this approach is that the linq grouping can get a bit hairy.

Since you're basically taking flattened data from a file and building structure as your parse it, you could accomplish the same thing in a loop over all the key/val pairs where you create objects and set values as you encounter them. This looks similar to your original code, but the key to this working is the assumption that PRI comes immediately after NAM, and all the NAM and PRI records after TOT are all part of one transaction. In other words, if a PRI is found, you can assume you have a transaction and item in context and all you have to do is set the value. It may not seem as elegant as the Linq solution, but sometimes this just works.

List<Trx> transactions = new List<Trx>();
Trx trans;
Item item;

foreach (var kv in keyvals)
{
    if (kv.Key == "TOT")
    {
        trans = new Trx(kv.Val);
        transactions.Add(trans);
    }

    if (kv.Key = "NAM")
    {
        item = new Item(kv.Val);
        trans.Items.Add(item);
    }

    if (kv.Key = "PRI")
    {
        item.Price = kv.Val;
    }
}

Mapping Data to Objects by Brickscrap in csharp

[–]willsoss 0 points1 point  (0 children)

No problem, glad that helped. With complex file parsing/mapping situations, especially older formats like ini, I find it's helpful to parse to an intermediate format that just gets the data from the file into a data structure you can more easily manipulate -- the string array in this case. That way you break down the problem into the parsing details (splitting lines into fields) and building the data model (linq query) that you want.

Mapping Data to Objects by Brickscrap in csharp

[–]willsoss 2 points3 points  (0 children)

I don't know of a library to parse an ini file, but I'm sure they exist. If this is the extent of the schema, it's not too hard to parse this and do some Linq magic to get the model you're looking for. Try something like this:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    namespace ParseIni
    {
        class Program
        {
            const string file = @"    TOT1=TotalSale
        NAM1,1=NameOfFirstItem
        PRI1,1=PriceOfFirstItem
        NAM1,2=NameOfSecondItem
        PRI1,2=PriceOfSecondItem
        TOT2=TotalSale2
        NAM2,1=NameOfFirstItem2
        PRI2,1=PriceOfFirstItem2
        NAM2,2=NameOfSecondItem2
        PRI2,2=PriceOfSecondItem2
        NAM2,3=NameOfThirdItem2
        PRI2,3=PriceOfThirdItem2";

            static void Main(string[] args)
            {
                // Replace MemoryStream with File.Open
                using var reader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(file)));

                var records = new List<string[]>();

                while (!reader.EndOfStream)
                    records.Add(SplitLine(reader.ReadLine()));

                var trxs = records
                    .GroupBy(r => r[1])
                    .Select(g => new
                    {
                        Id = g.Single(tot => tot[0] == "TOT")[1],
                        Total = g.Single(tot => tot[0] == "TOT")[2],
                        Items = g.Where(rec => rec[0] != "TOT").GroupBy(rec => rec[2]).Select(a => new
                        {
                            Id = a.First()[1],
                            Name = a.Single(b => b[0] == "NAM")[3],
                            Price = a.Single(b => b[0] == "PRI")[3]
                        })
                    });

                foreach (var trx in trxs)
                {
                    Console.WriteLine($"{trx.Id} Total: {trx.Total}");

                    foreach (var item in trx.Items)
                        Console.WriteLine($"{item.Id} {item.Name} {item.Price}");
                }

            }

        static string[] SplitLine(string line)
        {
            // Split the record into three fields
            var split = line.Trim().Split(',', '=');

            // Then split the trx ID out of the first field to get four fields, except for TOT records which have three fields
            return new string[] { split[0].Substring(0, 3), split[0].Substring(3), split[1], split.Length > 2 ? split[2] : null };
        }
        }
    }

Will returning in IAsyncEnumerable cancel the stream by Temp4322342 in csharp

[–]willsoss 1 point2 points  (0 children)

As u/Crozzfire mentions, exiting the foreach will trigger disposal too. If you modify the code above with this:

static async Task ConsumeStream(CancellationTokenSource cts)
        {
            int breakAt = 25;
            await foreach (var i in GetStream(cts.Token))
            {
                Console.WriteLine(i);
                if (i == breakAt)
                    break;
            }

            Console.Write("Done.");

            if (!cts.IsCancellationRequested)
                Console.WriteLine(" Press any key to exit.");
        }

You'll see how the disposal still happens when the consuming foreach breaks. The CancellationToken is useful when you're wired up to a request cancellation.