小编Fab*_*ano的帖子

linq扩展方法从序列的末尾获取元素

有可枚举的扩展方法

Take<TSource>(
    IEnumerable<TSource> source,
    int count
)
Run Code Online (Sandbox Code Playgroud)

count从一开始就采用第一个元素.

有没有办法从最后获取元素?甚至更好的方法从偏移到结束采取元素?

谢谢

.net c# linq ienumerable linq-extensions

7
推荐指数
1
解决办法
3134
查看次数

实体框架:如何在修改模型时更新数据库

在Entity Framework 4中,有"从数据库更新模型"和"从模型生成数据库"选项.但我缺少的是一个选项

从模型更新数据库

它通过修改数据库模式(例如添加新列)反映模型中所做的更改(例如,添加新的Property或Navigation-Property).不失其内容.

有人知道实现这一目标的方法,还是有一个t4模板可以在不删除现有表的情况下执行模式更新?(我使用的是Visual Studio 2010,.Net 4.0和SQL Server 2008)

谢谢

c# sql-server ado.net entity-framework entity-framework-4

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

仅在从特定操作重定向时才允许访问操作

是否有一种限制对操作的访问权限的好方法,因此如果您从另一个操作重定向,则只能访问它.例如:

    [HttpPost]
    public virtual ActionResult Create(MyViewModel vm)
    {            
        if (ModelState.IsValid)
        {
            // do some work

            return RedirectToAction("CreateSuccess");
        }
        else
        {
            return View(vm);
        }
    }


    public virtual ActionResult CreateSuccess()
    {
        // only allow execution if you were redirected from Action "Create" 
    }
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net-mvc asp.net-mvc-2

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

在哪个命名空间/包中放置异常

构建异常类位置的常见或最佳实践是什么?

假设您有包/名称空间myproject.person(人员的模型和DAO)和myproject.order(订单的模型和DAO)以及例外PersonExceptionOrderException.我应该将异常放在相应的包中还是在单独的包中用于例外(例如myproject.exceptions)?

第一种方法似乎更合理(因为它按功能排序).但是问题出在哪里你应该放置与两者相关的例外?例如ConstraintViolationException

谢谢

c# java exception package-structuring namespace-organisation

5
推荐指数
3
解决办法
2171
查看次数

将空字符串绑定到Guid.Empty或避免模型状态错误

当我为一个Guid字段发布一个带有空字符串""的表单时,我收到错误"MyGuid字段是必需的".虽然我没有设置"必需"属性.

//NOT Required   
public Guid MyGuid { get; set; }
Run Code Online (Sandbox Code Playgroud)

在模型绑定之后,Guid是00000000-0000-0000-0000-000000000000(因为它是默认值)并且这是正确的.但ModelState有上述错误.

我怎样才能避免这个错误?

附加信息:

[Required(AllowEmptyStrings = true)] 没有帮助

我不想让Guid为nullable(Guid?),因为这会导致很多额外的代码(检查它是否有值,映射等)

更新:

好的,我发现Guid?在我的视图模型中的更改不会导致比我预期的更多更改(一些调用MyGuid.GetValueOrDefault()或一些检查MyGuid.HasValue和调用MyGuid.Value).

但是,如果没有为post请求提供有效的Guid,则添加模型错误的原因是DefaultModelBinder尝试绑定nullGuid.解决方案是覆盖DefaultModelBinder.并且不会在模型状态中添加任何错误

public class MyModelBinder : DefaultModelBinder
{
    protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
    {
        if (propertyDescriptor.PropertyType == typeof(Guid) && value == null)
        {
            value = Guid.Empty;
        }
        base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);

    } …
Run Code Online (Sandbox Code Playgroud)

c# validation asp.net-mvc model-binding asp.net-mvc-2

5
推荐指数
1
解决办法
7039
查看次数

在Entity Framework 4中显式地显示多个到多个连接表

默认情况下,EF隐藏多对多连接表,该连接表不包含除连接表的外键之外的其他数据.

是否有可能告诉EF(和设计者)显式创建连接表并使其在代码中可用?

谢谢

.net c# ria entity-framework entity-framework-4

5
推荐指数
1
解决办法
2864
查看次数

CAML查询比较DateTime与Eq

我正在尝试组合一个比较两个DateTime对象的CAML查询,但我无法使用Eq比较来使其工作.根据我的测试,我可以让Gt,Lt,Geq,Leq使用DateTime比较,但Eq似乎根本不起作用.

第一个对象是日期和时间字段(由InfoPath生成并保存到SharePoint列表中的日期和时间字段),当前示例具有"3/14/2012 12:00 AM".我尝试使用[Today /]值,使用ISO格式2012-03-14T00:00:00Z中的硬编码值,但到目前为止还没有任何工作.我已经尝试了IncludeTimeValue,将其设置为true/false,没有任何改进.

我当前的查询看起来有点像这样,

<Query>
 <Where>
  <Eq>
   <FieldRef Name="SomeDateTimeField" IncludeTimeValue="TRUE" />
   <Value Type="DateTime" IncludeTimeValue="TRUE">2012-03-14T00:00:00Z</Value>
  </Eq>
 </Where>
</Query>
Run Code Online (Sandbox Code Playgroud)

即使我在列表中有一个具有该日期时间的项目,也不会返回任何内容.有任何想法吗?

.net sharepoint infopath caml

5
推荐指数
1
解决办法
1万
查看次数

C#合并父级别对象列表中的子对象列表

如果您有一个List并且想要为具有相同Id字段的任何SomeObject合并子List,您将如何做到这一点?以下是示例对象:

public class SomeObject
{
    public string Name { get; set; }
    public int Id { get; set; }
    public List<KeyPair> ValuePairs {get;set;} 
}

public class KeyPair
{
    public string Key { get; set; }
    public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是模拟列表的示例创建:

List<SomeObject> objects = new List<SomeObject>();
        objects = new List<SomeObject>()
        {
            new SomeObject
            {
                Name="Rando Object 1",
                Id=5,
                ValuePairs=new List<KeyPair>()
                {
                    new KeyPair
                    {
                        Key="TestKey1",
                        Value="TestValue1"
                    },
                    new KeyPair
                    {
                        Key="TestKey2",
                        Value="TestValue2"
                    }
                }
            },
            new SomeObject …
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-objects

5
推荐指数
1
解决办法
1691
查看次数

在Spring.Net中将Property用作工厂方法

有没有一种方法可以将类的静态属性用作对象定义的工厂方法?

  <object id="MyObject" type="MyNamespace.Factory, MyAssembly" factory-method="FactoryObject"  /> 
  <!-- "FactoryObject" is a Property (with getter) of the class "Factory" -->
Run Code Online (Sandbox Code Playgroud)

使用此配置会引发异常:

创建上下文'spring.root'时出错:类型[MyNamespace.Factory]上找不到匹配的工厂方法'FactoryObject

.net c# spring.net

4
推荐指数
1
解决办法
1822
查看次数

通用 Linq 查询 NotSupportedException:无法解析表达式

我试图通过一个帮助类来过滤 Entity Framework 核心中的一些结果,但我收到了这个错误,我不知道为什么:

NotSupportedException: Could not parse expression 'value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable'1[eVendCustomerDAL.DomainModels.Bin]).Any(__funcTest_0)': The given arguments did not match the expected arguments: Object of type 'System.Linq.Expressions.TypedParameterExpression' cannot be converted to type 'System.Linq.Expressions.LambdaExpression'.

这就是发生错误的地方:

    public virtual Revision<DTO> GetStateAsRevision(
        Expression<Func<Domain, bool>> query) {
        //get all the stateful data from the database
        DbSet<Domain> oriSet = db.Set<Domain>();
        //query all the data
        List<Domain> oriList = oriSet.Where(query).ToList();
Run Code Online (Sandbox Code Playgroud)

该错误似乎是由于query我正在推送的参数而发生的,并且它仅在运行时发生。我不确定是否应该归咎于它的通用性。泛型也是类而不是接口。

where DTO : BaseRevisionDTO
where Domain : BaseTrackedObject
where DomainRevision : BaseRevision
Run Code Online (Sandbox Code Playgroud)

然后我使用以下片段构建表达式:

    private Expression<Func<Domain, bool>> GetDomainSearchExpression(Func<Domain, bool> shortFunction) …
Run Code Online (Sandbox Code Playgroud)

c# generics linq-to-entities entity-framework

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