小编Dan*_* T.的帖子

返回两个数字中最大的一个最简单的方法是什么?

假设我有一个看起来像这样的实体:

public class Album()
{
    public DateTime LastUpdated { get; set; }
    public List<Picture> Pictures { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是创建一个LastActivity属性,该属性将返回活动的最新日期.这对于Pictures集合来说很容易:

public DateTime LastActivity
{
    get { return Pictures.Max(x => x.LastUpdated); }
}
Run Code Online (Sandbox Code Playgroud)

但是,我也想考虑实体LastUpdated上的属性Album.我可以使用这段代码:

public DateTime LastActivity
{
    get { return Pictures.Max(x => x.LastUpdated) > this.LastUpdated
              ? Pictures.Max(x => x.LastUpdated)
              : this.LastUpdated) };
}
Run Code Online (Sandbox Code Playgroud)

但这很糟糕,因为它会进行Max()两次转换.有没有更好的方法来编写这段代码?

回答

根据接受的答案,这是我提出的解决方案:

public virtual DateTime LastActivity
{
    get
    {
        var max = Pictures.Any() ? Pictures.Max(x …
Run Code Online (Sandbox Code Playgroud)

c# linq collections comparison max

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

ColdFusion是否支持代表?

我有几个数据库访问方法包含在try/catch块中:

function GetAll() {
    try {
        entityLoad("Book");
    }
    catch (any e) {
        throw (type="CustomException", message="Error accessing database, could not read");
    }
}

function Save(Book book) {
    try {
        entitySave(book);
    }
    catch (any e) {
        throw (type="CustomException", message="Error accessing database, could not save);
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,try/catch块会重复多次,其中唯一不同的是消息.是否可以在ColdFusion中创建委托,以便我可以做这样的事情(使用C#lambda来表示匿名委托)?:

function GetAll() {
    DatabaseOperation(() => entityLoad("Book"), "could not read");
}

function Save(Book book) {
    DatabaseOperation(() => entitySave(book), "could not save");
}

function DatabaseOperation(delegate action, string error) {
    try {
        action.invoke();
    }
    catch (any e) { …
Run Code Online (Sandbox Code Playgroud)

coldfusion delegates dry coldfusion-9

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

控制器构造函数调用每个请求

我正在尝试测试一个只使用列表和创建的非常简单的表单.这是控制器:

public class PositionsController : Controller
{
    private readonly IPositionRepository _positions;

    // default constructor
    public PositionsController()
    {
        _positions = new TestPositionRepository();
    }

    // DI constructor
    public PositionsController(IPositionRepository positions)
    {
        _positions = positions;
    }

    // get a list of all positions
    public ActionResult Index()
    {
        return View(_positions.GetAllPositions());
    }

    // get initial create view
    public ActionResult Create()
    {
        return View();
    } 

    // add the new Position to the list
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(Position positionToAdd)
    {
        try
        {
            _positions.AddPosition(positionToAdd);

            return RedirectToAction("Index");
        }
        catch …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc constructor controller httpwebrequest

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

什么是数据库对象的工作单元?

用外行人的话来说,关于数据库对象的工作单元是什么?我正在研究如何将数据库表转换为C#类,我经常遇到这个术语,但每个人似乎都在描述它,好像你应该已经知道它是什么.

database database-design unit-of-work

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

用虚拟数据填充POCO的最佳方法是什么?

我有一堆POCO,它们都在一棵大树上相互关联.例如,这是顶级元素:

public class Incident : Entity<Incident>
{
    public virtual string Name { get; set; }
    public virtual DateTime Date { get; set; }
    public virtual IEnumerable<Site> Sites { get; set; }

    public Incident()
    {
        Sites = new HashSet<Site>();
    }
}
Run Code Online (Sandbox Code Playgroud)

树就像这样Incident -> Sites -> Assessments -> Subsites -> Images.POCO没有任何逻辑,只是一堆属性.我想要做的就是用随机虚拟数据填充每个属性,这样我就可以编写一些搜索代码.如果我想创建大量的虚拟数据,最好的方法是什么?

asp.net-mvc mocking stub poco

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

让通用扩展方法正常工作的问题

我正在尝试为HashSet创建扩展方法AddRange,所以我可以这样做:

var list = new List<Item>{ new Item(), new Item(), new Item() };
var hashset = new HashSet<Item>();
hashset.AddRange(list);
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止:

public static void AddRange<T>(this ICollection<T> collection, List<T> list)
{
    foreach (var item in list)
    {
        collection.Add(item);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试使用AddRange时,我收到此编译器错误:

The type arguments for method 'AddRange<T>(System.Collections.Generic.ICollection<T>, System.Collections.Generic.List<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

换句话说,我必须最终使用它:

hashset.AddRange<Item>(list);
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

c# generics extension-methods list hashset

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

如何编写将不同类型作为参数的泛型方法?

我有以下扩展方法将一个集合中的元素添加到另一个集合:

public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> list)
{
    foreach (var item in list)
    {             
        collection.Add(item);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果IEnumerable列表与我试图将其添加到的ICollection的类型相同,则此方法可以正常工作.但是,如果我有这样的事情:

var animals = new List<Animal>();
var dogs = new List<Dog>(); // dog is a subclass of animal
animals.AddRange(dogs); // this line has a compiler error, it can't infer the type
Run Code Online (Sandbox Code Playgroud)

如果IEnumerable的类型是T类型的子类(或实现接口),如何修改我的扩展方法以便能够执行此类操作?

c# generics parameters extension-methods

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

如何使用Fluent NHibernate自动映射字典?

我有一个像这样的实体:

public class Land
{
    public virtual IDictionary<string, int> Damages { get; set; }
    // and other properties
}
Run Code Online (Sandbox Code Playgroud)

每次我尝试使用以下代码自动化:

var sessionFactory = Fluently.Configure()
    .Database(SQLiteConfiguration.Standard.InMemory)
    .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Land>))
    .BuildSessionFactory();
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

{"The type or method has 2 generic parameter(s), but 1 generic argument(s) were
provided. A generic argument must be provided for each generic parameter."}
Run Code Online (Sandbox Code Playgroud)

有人能告诉我我做错了什么吗?此外,这只是一个简单的例子.我有更多的词典而不仅仅是这个词典.

configuration dictionary fluent-nhibernate automapping

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

如何删除ColdFusion ORM中的关联?

假设我有两个实体,a House:

component
{
    property name="Owner" cfc="Owner" fieldtype="many-to-one";
}
Run Code Online (Sandbox Code Playgroud)

并且Owner:

component
{
    property name="Name";
}
Run Code Online (Sandbox Code Playgroud)

A House可能有Owner,但不需要.我有一个House关联Owner,但现在我想删除该关联.我试过以下代码:

 var house = entityLoadByPK("House", 13);
 house.setOwner('');
 entitySave(house);
Run Code Online (Sandbox Code Playgroud)

但我得到一个例外,说''无法转换为id,这是有道理的.但是,虽然ColdFusion有一个null/undefined的概念,但看起来你实际上不能创建一个空值,只检查它们.在这种情况下,我如何删除关联?

coldfusion orm null hibernate coldfusion-9

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

<%= @something%>在Java属性文件中的含义是什么?

我正在查看使用外部配置文件的Grails项目.我有一个外部配置文件,它是一个Java属性文件,我不知道这行是做什么的:

environment.name = <%= @envname %>
Run Code Online (Sandbox Code Playgroud)

<%= @envname %>什么,以及@envname属性设置在哪里?

java grails configuration-files

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