小编ben*_*asd的帖子

EntityFramework查询操作,db提供程序包装,db表达式树

我正在尝试为Entity Framework实现数据本地化逻辑.因此,如果例如查询选择Title属性,则在幕后它应该引用列Title_enGBTitle_deCH取决于当前用户文化.

为此,我想从Entity Framework重写DbExpression CommandTrees.我以为这些是建立跨数据库插入/更新/ select查询.但现在所有相关的构造函数/在命名空间中的工厂新的通用.NET的方式System.Data.MetadataSystem.Data.Common.CommandTreesSystem.Data.Entity.dll都内!(在msdn中公开记录,如:)DbExpressionBuilder.

有没有人有想法实现这个查询操作有或没有查询树重写?

我想要的代码:( public class DbProviderServicesWrapper : DbProviderServices)

/// <summary>
/// Creates a command definition object for the specified provider manifest and command tree.
/// </summary>
/// <param name="providerManifest">Provider manifest previously retrieved from the store provider.</param>
/// <param name="commandTree">Command tree for the statement.</param>
/// <returns>
/// An exectable command definition object.
/// </returns>
protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, …
Run Code Online (Sandbox Code Playgroud)

c# expression localization expression-trees entity-framework-4

14
推荐指数
2
解决办法
2615
查看次数

C#,objectCollection.OfType <T>()和foreach(objectCollection中的T项),IEnumerable到IEnumerable <T>

以我个人的编码风格所属Enumerable.OfType<T>().我在任何地方使用它都有点意义.特别是IEnumerable<T>允许强大的linqToObject功能.我讨厌ObjectCollections的"类型不安全"循环,例如下面的示例.现在我有一些关于循环这些"UnGenericCollections"的问题.

问题1:如果我将此转换ArrayListEnumerable<T>与简单的foreach/if-checks相比有多大的额外循环?

var ark = new ArrayList();
ark.Add(new Human());
ark.Add(new Human());
ark.Add(new Animal());
Run Code Online (Sandbox Code Playgroud)

代替:

foreach (object passenger in ark)
{
    if (passanger is Human) { }
    if (passanger is Animal) { }
}
Run Code Online (Sandbox Code Playgroud)

我用:

foreach (var human in ark.OfType<Human>())
{
}

foreach (var animal in ark.OfType<Animal>())
{
}
Run Code Online (Sandbox Code Playgroud)

问题2:在foreach循环中使用不同类型的变量时,将使用哪种投射/转换方式?这是一种语言功能还是开箱即用?

foreach (Human human in ark) { }
Run Code Online (Sandbox Code Playgroud)

谢谢你忍受我可怕的英语.最好的问候,本杰明

c# ienumerable foreach linq-to-objects generic-list

6
推荐指数
1
解决办法
2967
查看次数

System.Action <T>作为EventHandler

相信使用委托System.Action或System.Func作为EventDelegates而不是经典的EventHandler模​​式.因此我会遇到问题吗?

private bool disposed;

public event Action<IUnitOfWork, IContext> Disposing;

public void Dispose()
{
    if (this.disposed)
    {
        return;
    }

    if (null != this.Disposing)
    {
        this.Disposing(this, this.AttachedContext);
    }

    this.disposed = true;
}
Run Code Online (Sandbox Code Playgroud)

-

用法:

unitOfWorkInstance.Disposing += (u, c) => c.Rollback(u); // in my opinion more readable than
unitOfWorkInstance.Disposing += (sender, args) => args.AttachedContext.Rollback(sender as IUnitOfWork);
Run Code Online (Sandbox Code Playgroud)

抱歉可怕的englisch

.net c# action func event-handling

2
推荐指数
1
解决办法
5863
查看次数

隐式/显式转换运算符未被属性调用(System.ComponentModel.DataAnnotation.dll)

我们在域模型中使用了自定义LocalizedString类型.我们想用验证属性来装饰属性MaxLength.为此,我们添加了隐式运算符以启用此属性所需的强制转换.

奇怪的是,运算符似乎永远不会被调用,并且在属性IsValid方法中抛出了InvalidCastException .在我们自己的项目中执行此演员表.

在这个系统clr ngen'ed属性中有没有特殊的强制转换行为编译器magix?

// Custom type
public class LocalizedString
{
    public string Value
    {
        get { return string.Empty; }
    }

    public static implicit operator Array(LocalizedString localizedString)
    {
        if (localizedString.Value == null)
        {
            return new char[0];
        }

        return localizedString.Value.ToCharArray();
    }
}

// Type: System.ComponentModel.DataAnnotations.MaxLengthAttribute
// Assembly: System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.ComponentModel.DataAnnotations.dll
public override bool IsValid(object value)
{
  this.EnsureLegalLengths();
  if (value == null)
  {
    return true;
  }
  else
  {
    string str = value …
Run Code Online (Sandbox Code Playgroud)

c# implicit-conversion data-annotations explicit-conversion

2
推荐指数
1
解决办法
1210
查看次数