Mongo и C#: агрегация, pipeline

Очень старый кусок кода (~2012-13 гг.), но вдруг пригодится когда-нибудь, память освежить.

            var match = new BsonDocument 
            { { 
                    "$match", new BsonDocument 
                    {         
                        {
                            "Date", new BsonDocument 
                            { { "$lte", DateTime.Now } }
                        }
                    } 
            } };
            var group = new BsonDocument
            { {
                    "$group", new BsonDocument
                    {
                        { "_id","$Blog._id" },
                        {
                            "PublishDate", new BsonDocument 
                            { { "$max", "$Date" } }
                        }
                    }
            } };

            var project = new BsonDocument
            { {
                    "$project", new BsonDocument
                    {
                        { "Blog._id", "$_id" },
                        { "PublishDate", "$PublishDate" }
                    }
            } };

            var sort = new BsonDocument { { "$sort", new BsonDocument { { "PublishDate", -1 } } } };
            var skip = new BsonDocument { { "$skip", (page - 1) * count } };
            var limit = new BsonDocument { { "$limit", count } };

            var pipeline = new[] { match, group, sort, project, skip, limit };
            var lastPostsInBlogs = this.mongoContext.Posts
                .Aggregate(pipeline)
                .ResultDocuments.Select(x => BsonSerializer.Deserialize(x))
                .ToList();

            return
                lastPostsInBlogs.Join(
                    this.mongoContext.Blogs.AsQueryable()
                        .Where(condition),
                    pb => pb.Blog.Id,
                    b => b.Id,
                    (pb, b) =>
                    new BlogItem()
                    {
                        Id = b.StrId,
                        Alias = b.Alias,
                        Title = b.Title,
                        Description = b.Info,
                        CoverFile = b.CoverFile ?? DomainAppSettings.DefaultPreview,
                        CanPost = b.Owner.AccountId == visitorId || (b.IsCommon && b.Members.Contains(visitorId)),
                        LastUpdated = b.LastUpdated,
                        LastPosts = this.mongoContext.Posts
                            .AsQueryable()
                            .Where(p => p.Blog.Id == b.Id && p.PublishDate  p.PublishDate)
                            .Take(3)
                            .Select(p => new PostLine()
                            {
                                Id = p.StrId,
                                Number = p.Number,
                                BlogAlias = p.Blog.Alias,
                                Title = p.Title,
                                Date = p.Date
                            })
                    }).ToArray();

Передача массива в Asp.NET через ajax

http://stackoverflow.com/questions/309115/how-can-i-post-an-array-of-string-to-asp-net-mvc-controller-without-a-form

function test()
{
    var stringArray = new Array();
    stringArray[0] = "item1";
    stringArray[1] = "item2";
    stringArray[2] = "item3";
    var postData = { values: stringArray };

    $.ajax({
        type: "POST",
        url: "/Home/SaveList",
        data: postData,
        success: function(data){
            alert(data.Result);
        },
        dataType: "json",
        traditional: true
    });
}
public JsonResult SaveList(List values)
{
    return Json(new { Result = String.Format("Fist item in list: '{0}'", values[0]) });
}

Про MVC UI

Генерация на основе темплейтов
http://habrahabr.ru/post/165025/

JTable
http://www.jtable.org/Home/Documents
Доки
http://www.codeproject.com/Articles/277576/AJAX-based-CRUD-tables-using-ASP-NET-MVC-and-jTa#View

Подборка библиотек для таблиц

List of ASP.NET MVC grid controls

MVC Contrib
Доки http://mvccontrib.codeplex.com/wikipage?title=Grid&referringTitle=Documentation
Исходники http://mvccontrib.codeplex.com/SourceControl/latest#src/MVCContrib/UI/Grid/IGridModel.cs

Grid.Mvc
http://gridmvc.codeplex.com/

EF и Generic

Оставляю кусочек кода себе на память. Универсальный, но в моем случае требует костылей.

#region Generic
        public void CreateLang(TModel langModel, out IEnumerable diff)
        {
            Type entityType = this.TypesModelEntity[typeof(TModel)]; //вот костыль
            var langType = this.TypesEntityLang[entityType]; //и еще один

            Type genericClass = typeof(EFLangRepository);
            Type constructedClass = genericClass.MakeGenericType(typeof(TModel), entityType, langType);

            object repo = Activator.CreateInstance(constructedClass);

            diff = new List();
            object[] args = new object[] { langModel, diff };

            MethodInfo magicMethod = constructedClass.GetMethod("CreateLang");
            object magicValue = magicMethod.Invoke(repo, args);
            diff = args[1] as List;
        }
        #endregion

Continue Reading →

Работа с полями класса

http://stackoverflow.com/questions/9210428/how-to-convert-class-into-dictionarystring-string

someObject.GetType()
     .GetProperties(BindingFlags.Instance | BindingFlags.Public)
          .ToDictionary(prop => prop.Name, prop => prop.GetValue(someObject, null))

http://stackoverflow.com/questions/4943817/mapping-object-to-dictionary-and-vice-versa/4944547#4944547

public static class ObjectExtensions
{
    public static T ToObject(this IDictionary source)
        where T : class, new()
    {
            T someObject = new T();
            Type someObjectType = someObject.GetType();

            foreach (KeyValuePair item in source)
            {
                someObjectType.GetProperty(item.Key).SetValue(someObject, item.Value, null);
            }

            return someObject;
    }

    public static IDictionary AsDictionary(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
    {
        return source.GetType().GetProperties(bindingAttr).ToDictionary
        (
            propInfo => propInfo.Name,
            propInfo => propInfo.GetValue(source, null)
        );

    }
}

class A
{
    public string Prop1
    {
        get;
        set;
    }

    public int Prop2
    {
        get;
        set;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Dictionary dictionary = new Dictionary();
        dictionary.Add("Prop1", "hello world!");
        dictionary.Add("Prop2", 3893);
        A someObject = dictionary.ToObject();

        IDictionary objectBackToDictionary = someObject.AsDictionary();
    }
}

http://stackoverflow.com/questions/2051834/exclude-property-from-gettype-getproperties

public class SkipPropertyAttribute : Attribute
{
}

public static class TypeExtensions
{
    public static PropertyInfo[] GetFilteredProperties(this Type type)
    {
        return type.GetProperties().Where(pi => pi.GetCustomAttributes(typeof(SkipPropertyAttribute), true).Length == 0).ToArray();
    }       
}

public class Test
{
    public string One { get; set; }

    [SkipProperty]
    public string Two { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var t = new Test();
        Type ty = t.GetType();

        PropertyInfo[] pinfo = ty.GetFilteredProperties();
        foreach (PropertyInfo p in pinfo)
        {
            Console.WriteLine(p.Name);
        }

        Console.ReadKey();
    }
}

http://stackoverflow.com/questions/4951233/compare-two-objects-and-find-the-differences

static class extentions
{
    public static List DetailedCompare(this T val1, T val2)
    {
        List variances = new List();
        FieldInfo[] fi = val1.GetType().GetFields();
        foreach (FieldInfo f in fi)
        {
            Variance v = new Variance();
            v.Prop = f.Name;
            v.valA = f.GetValue(val1);
            v.valB = f.GetValue(val2);
            if (!v.valA.Equals(v.valB))
                variances.Add(v);

        }
        return variances;
    }


}
class Variance
{
    public string Prop { get; set; }
    public object valA { get; set; }
    public object valB { get; set; }
}

LINQ Join vs. Contains

Хозяйке на заметку.
В данном примере всё выполняется в памяти приложения.

http://stackoverflow.com/questions/16824510/select-multiple-records-based-on-list-of-ids-with-linq

Solution with .Where and .Contains has complexity of O(N square). Simple .Join should have a lot better performance (close to O(N) due to hashing). So the correct code is:

_dataContext.UserProfile.Join(idList, up => up.ID, id => id, (up, id) => up);

And now result of my measurement. I generated 100 000 UserProfiles and 100 000 ids. Join took 32ms and .Where with .Contains took 2 minutes and 19 seconds! I used pure IEnumerable for this testing to prove my statement. If you use List instead of IEnumerable, .Where and .Contains will be faster. Anyway the difference is significant. The fastest .Where .Contains is with Set. All it depends on complexity of underlying coletions for .Contains. Look at this post to learn about linq complexity.Look at my test sample below:

    private static void Main(string[] args)
    {
        var userProfiles = GenerateUserProfiles();
        var idList = GenerateIds();
        var stopWatch = new Stopwatch();
        stopWatch.Start();
        userProfiles.Join(idList, up => up.ID, id => id, (up, id) => up).ToArray();
        Console.WriteLine("Elapsed .Join time: {0}", stopWatch.Elapsed);
        stopWatch.Restart();
        userProfiles.Where(up => idList.Contains(up.ID)).ToArray();
        Console.WriteLine("Elapsed .Where .Contains time: {0}", stopWatch.Elapsed);
        Console.ReadLine();
    }

    private static IEnumerable GenerateIds()
    {
       // var result = new List();
        for (int i = 100000; i > 0; i--)
        {
            yield return i;
        }
    }

    private static IEnumerable GenerateUserProfiles()
    {
        for (int i = 0; i < 100000; i++)
        {
            yield return new UserProfile {ID = i};
        }
    }

Console output:

Elapsed .Join time: 00:00:00.0322546
Elapsed .Where .Contains time: 00:02:19.4072107

А вот про SQL

http://stackoverflow.com/questions/1200295/sql-join-vs-in-performance

If the joining column is UNIQUE and marked as such, both these queries yield the same plan in SQL Server.

If it’s not, then IN is faster than JOIN on DISTINCT.