1

I need to be able to perform a string based, bitwise, .OrderBy in dynamic linq.

items = items.OrderBy(x => (x.Permissions & 65536) != 0);

vs.

items = items.Where("1==1" + where).OrderBy("Permissions & 65536 != 0")

Is this even possible? I don't see any examples for bitwise operators in the download (CSharpSamples.zip\LinqSamples\DynamicQuery). I can't get anything to work and I can't find any examples. Help!

michaelb
  • 149
  • 2
  • 11
  • You are anding by 0x10000 hex. Shouldn't you be anding by 0xFFFF? – jdweng Oct 23 '18 at 21:47
  • I'm not concerned with the semantics of the example, just the ability to actually perform a string based OrderBy. In Dynamic Linq that "&" is used for string concat, which doesn't help me. I'm looking for a working example of how to do this. – michaelb Oct 23 '18 at 22:25
  • 1
    I dont think it supports bitwise operations, you might have to mix and match, also, dynamic linq is a little well. meh.. and a refactoring nightmare, is there any reason you cant just build up expressions? or the query programmatically – TheGeneral Oct 23 '18 at 23:10
  • I agree. It doesn't look like it is supported. See: https://github.com/kahanu/System.Linq.Dynamic/issues/16 I'm using dynamic linq to allow for dynamic searching/pagination from a genericized api. This was the best solution at the time, and I've yet to find or research an alternative. If you want to post your comment as an answer I'll mark is as such. – michaelb Oct 23 '18 at 23:19

1 Answers1

1

You can reorder the columns of the data base on string inputs. The code below is tested and works.

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

namespace ConsoleApplication75
{
    class Program
    {
         static void Main(string[] args)
        {
            //u = user
            //g = group
            //s = system
            //r = read
            //w = write
            //e = execute

            //array below represents the normal position of the permissions
            List<string> permissions = new List<string>() { "ue", "uw", "ur", "ge", "gw", "gr", "se", "sw", "sr" };

            //below is the order we actually want
            List<string> sortOrder = new List<string>() { "ue", "ge", "se", "uw", "gw", "sw", "ur", "gr", "sr" };

            List<cFile> files = new List<cFile>() {
                new cFile() { name = "A", permission = 0x001},
                new cFile() { name = "B", permission = 0x002},
                new cFile() { name = "C", permission = 0x004},
                new cFile() { name = "D", permission = 0x008},
                new cFile() { name = "E", permission = 0x010},
                new cFile() { name = "F", permission = 0x020},
                new cFile() { name = "G", permission = 0x040},
                new cFile() { name = "H", permission = 0x080},
                new cFile() { name = "I", permission = 0x100}
            };


            //now reorder the columns
             var tempData = files.Select(x => new { files = x, shiftedPermissions =  Enumerable.Range(0, 9).Select(y => (permissions.IndexOf(sortOrder[y]) - y >= 0) ?
                (1 << y) & (x.permission >> (permissions.IndexOf(sortOrder[y]) - y)) :
                (1 << y) & (x.permission << (y - permissions.IndexOf(sortOrder[y])))).Sum()
             }).ToList();

             List<cFile> sortedPermissions = tempData.OrderBy(x => x.shiftedPermissions).Select(x => x.files).ToList();

        }

    }
    public class cFile
    {
        public string name { get; set; }
        public UInt32 permission { get; set; }
    }


}
jdweng
  • 33,250
  • 2
  • 15
  • 20