小编Kri*_*s-I的帖子

检查值是否在LINQ集合中

我有一个"Employee"类,它有一个IList <> of"TypeOfWork".

public class Employee
{
    public virtual IList<TypeOfWork> TypeOfWorks { get; set; }
}

public class TypeOfWork
{
    public virtual Customer Customer { get; set; }

    public virtual Guid Id { get; set; }
    public virtual string Name{ get; set; }
    public virtual bool IsActive{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在保存之前,我很想知道"typeofwid"(一个Guid)是否已经存在于"TypeOfWorks"集合中.

我试过这个:

var res =  from p in employee.TypeOfWorks 
           where p.Id == new Guid("11111111-1111-1111-1111-111111111111") 
           select p ;
Run Code Online (Sandbox Code Playgroud)

并试过这个:

bool res = employee.TypeOfWorks.Where(f => f.Id == new Guid("11111111-1111-1111-1111-111111111111")).Count() != 0; …
Run Code Online (Sandbox Code Playgroud)

c# linq collections lambda

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

保存后显示相同的页面

我想显示一个带有某个字段的表单(示例中有一个),提交它,保存并显示同一页面并重置所有字段.我提交时的问题,我进行"保存"操作,但是当我显示视图时,表单仍然填写.

该模型 :

public class TestingModel
{
    public string FirstName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

    public class ChildController : Controller
{
    public ActionResult Index()
    {
        TestingModel model = new TestingModel();
        return View(model);
    }

    public ActionResult Save(TestingModel model)
    {
         Console.WriteLine(model.FirstName); //OK

        //Save data to DB here ...          

        TestingModel testingModel = new TestingModel() { FirstName = string.Empty };
        return View("Index", testingModel);
    }
}
Run Code Online (Sandbox Code Playgroud)

风景 :

@using (Html.BeginForm("Save", "Child",FormMethod.Post))
{
    @Html.TextBoxFor( m => m.FirstName)
   <input type="submit" id="btSave" />
}
Run Code Online (Sandbox Code Playgroud)

当Id调试到视图时,在"Immediat窗口"中,Model.FirstName = …

asp.net-mvc asp.net-mvc-3

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

Generic <T>怎么演员?

我有一个"Product"基类,其他一些类"ProductBookDetail","ProductDVDDetail"继承自这个类.我使用ProductService类对这些类进行操作.但是,我必须根据类型(书的ISBN,DVD的语言)进行检查.我想知道投射"productDetail"值的最佳方法,我在SaveOrupdate中收到.我尝试了GetType()并使用了(ProductBookDetail)productDetail但这不起作用.

谢谢,

var productDetail = new ProductDetailBook() { .... };
var service = IoC.Resolve<IProductServiceGeneric<ProductDetailBook>>();
service.SaveOrUpdate(productDetail);

var productDetail = new ProductDetailDVD() { .... };
var service = IoC.Resolve<IProductServiceGeneric<ProductDetailDVD>>();
service.SaveOrUpdate(productDetail);


public class ProductServiceGeneric<T> : IProductServiceGeneric<T>
{
    private readonly ISession _session;
    private readonly IProductRepoGeneric<T> _repo;
    public ProductServiceGeneric()
    {
        _session = UnitOfWork.CurrentSession;
        _repo = IoC.Resolve<IProductRepoGeneric<T>>();
    }
    public void SaveOrUpdate(T productDetail)
    {            
        using (ITransaction tx = _session.BeginTransaction())
        {
            //here i'd like ot know the type and access properties depending of the class
            _repo.SaveOrUpdate(productDetail); …
Run Code Online (Sandbox Code Playgroud)

c# generics casting ioc-container

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

ASP.NET MVC 3:验证获取消息列表

我有这个 :

public class Customer
{
    [DisplayName("Lastname"), StringLength(50)]
    [Required(ErrorMessage="My Error Message")]
    [NotEmpty()]
    public override string LastName { get; set; }

    [DisplayName("Firstname"), StringLength(50)]
    [Required(ErrorMessage="My Error Message 2")]
    [NotEmpty()]
    public override string FirstName{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在控制器中,我这样做:

if (!TryValidateModel(myCustomer))
{
  //HERE
  ....
}
Run Code Online (Sandbox Code Playgroud)

"HERE"在哪里,我想得到所有错误信息.

一些示例案例:

  1. 如果缺少"LastName",我想要" 我的错误信息 "
  2. 如果两者都是mising,我想得到一个List(或其他),其值为" My Error Message "和" My Error Message 2 "

任何的想法 ?

谢谢,

validation asp.net-mvc

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

如何将默认打开页面设置为区域的操作

我有项目结构(见下文).当我启动项目时,我收到此错误(见下文).你能告诉我我必须做的改变,我想在我开始项目时去MyAreas1\Home

谢谢,

错误消息: 未找到视图"索引"或其主节点,或者视图引擎不支持搜索的位置.搜索了以下位置:

~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

asp.net-mvc asp.net-mvc-3

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

并且还要在几个Expression <Func <T,bool >>之间:从范围引用

我有3个谓词,我想要AndAlso介于两者之间.我在板上发现了几个样本,但无法解决我的问题.

这些谓词是: Expression<Func<T, bool>>

我有这个代码:

Expression<Func<T, bool>> predicate1 = ......;
Expression<Func<T, bool>> predicate2 = ......;
Expression<Func<T, bool>> predicate3 = ......;
Run Code Online (Sandbox Code Playgroud)

我创建了一个扩展方法来创建一个"AndAlso":

public static Expression<Func<T, bool>> AndAlso<T>(
    this Expression<Func<T, bool>> expr, 
    Expression<Func<T, bool>> exprAdd)
{
    var param = Expression.Parameter(typeof(T), "p");
    var predicateBody = Expression.AndAlso(expr.Body, exprAdd.Body);
    return Expression.Lambda<Func<T, bool>>(predicateBody, param);

    //Tried this too
    //var body = Expression.AndAlso(expr.Body, exprAdd.Body);
    //return Expression.Lambda<Func<T, bool>>(body, expr.Parameters[0]);
}
Run Code Online (Sandbox Code Playgroud)

我用这样的:

var finalPredicate = predicate1
    .AndAlso<MyClass>(predicate2)
    .AndAlso<MyClass>(predicate3);
Run Code Online (Sandbox Code Playgroud)

谓词看起来像这样: 在此输入图像描述

当我在查询中使用时:

var res =  myListAsQueryable().Where(finalPredicate).ToList<MyClass>();
Run Code Online (Sandbox Code Playgroud)

我得到这个错误: 从范围''引用的'BuilderPredicate.MyClass'类型的变量'p',但它没有定义

你能告诉我什么是错的吗?

非常感谢,

.net c# lambda expression

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

使用Expression创建具有嵌套类的谓词

我有这个 :

public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ZipCode { get; set; }
}
public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int? Age { get; set; }
    public …
Run Code Online (Sandbox Code Playgroud)

.net c# linq expression predicate

6
推荐指数
2
解决办法
1578
查看次数

将ASP.NET MVC模型分配给AngularJS范围

在我的视图代码下面(javascript代码在视图中,只是用于测试的temp).

我想将ASP.NET MVC模型(@Model)分配给AngularJS范围($scope.person)

我怎样才能做到这一点 ?

谢谢,

风景

@model MyApp.Person

<script>
var myApp = angular.module('myApp', []);

myApp.controller('personController', ['$scope', '$http', function ($scope, $http) {
    $scope.person = ?????
}]);
</script>
Run Code Online (Sandbox Code Playgroud)

更新1: 我在JS文件中尝试了这段代码:

var myApp = angular.module('myApp', []);

myApp.controller('personController', ['$scope', '$http', function ($scope, $http) {
    $scope.person = @Html.Raw(window.person);
}]);
Run Code Online (Sandbox Code Playgroud)

在视图文件中:

<script>
    @{
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    }
    window.person = serializer.Serialize(Model);
</script>
Run Code Online (Sandbox Code Playgroud)

我得到2个错误:

ReferenceError: serializer is not defined (on windows)
window.person = serializer.Serialize(Model);

SyntaxError: illegal character (it's the @)
$scope.person …
Run Code Online (Sandbox Code Playgroud)

javascript asp.net-mvc asp.net-mvc-4 angularjs

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

Automapper、INamingConvention Camelcase 属性为带下划线的大写

我有两个类,一个是由 Entity Framework 生成的,另一个是我在任何地方都使用的类。

我的课 :

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

EF类:

public class PERSON
{
    public string FIRST_NAME { get; set; }
    public string LAST_NAME { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当源为PERSONto 时Person,我找到了解决方案,我没有找到Personto的解决方案PERSON(属性为大写和下划线分隔符)。

The solution for PERSON to Person :

Mapper.Initialize(x => x.AddProfile<Profile1>());
var res = Mapper.Map<PERSON, Person>(person);

public class UpperUnderscoreNamingConvention : INamingConvention
{
    private readonly …
Run Code Online (Sandbox Code Playgroud)

.net c# automapper

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

在VS2012中使用EF 4.4而不是5.0

我们将一些项目从VS2010迁移到VS2012但仍然使用.NET 4.0而不是 .NET 4.5

当我添加一个新的EDMX时,VS2012使用EF 5.0,但我想继续使用版本4.x这是第一点.

第二点版本5.0,继承自DbContext而不是版本4.x的ObjectContext.使用5.0版本,我没有看到任何方法将连接字符串作为参数传递.

如何在VS2012中使用版本4.x,具有相同的EDMX设计器行为?

谢谢,

.net c# orm entity-framework

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