假设我有一个看起来像这样的实体:
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) 我有几个数据库访问方法包含在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) 我正在尝试测试一个只使用列表和创建的非常简单的表单.这是控制器:
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) 用外行人的话来说,关于数据库对象的工作单元是什么?我正在研究如何将数据库表转换为C#类,我经常遇到这个术语,但每个人似乎都在描述它,好像你应该已经知道它是什么.
我有一堆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没有任何逻辑,只是一堆属性.我想要做的就是用随机虚拟数据填充每个属性,这样我就可以编写一些搜索代码.如果我想创建大量的虚拟数据,最好的方法是什么?
我正在尝试为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)
我在这做错了什么?
我有以下扩展方法将一个集合中的元素添加到另一个集合:
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类型的子类(或实现接口),如何修改我的扩展方法以便能够执行此类操作?
我有一个像这样的实体:
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)
有人能告诉我我做错了什么吗?此外,这只是一个简单的例子.我有更多的词典而不仅仅是这个词典.
假设我有两个实体,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的概念,但看起来你实际上不能创建一个空值,只检查它们.在这种情况下,我如何删除关联?
我正在查看使用外部配置文件的Grails项目.我有一个外部配置文件,它是一个Java属性文件,我不知道这行是做什么的:
environment.name = <%= @envname %>
Run Code Online (Sandbox Code Playgroud)
做<%= @envname %>什么,以及@envname属性设置在哪里?
c# ×3
asp.net-mvc ×2
coldfusion ×2
coldfusion-9 ×2
generics ×2
automapping ×1
collections ×1
comparison ×1
constructor ×1
controller ×1
database ×1
delegates ×1
dictionary ×1
dry ×1
grails ×1
hashset ×1
hibernate ×1
java ×1
linq ×1
list ×1
max ×1
mocking ×1
null ×1
orm ×1
parameters ×1
poco ×1
stub ×1
unit-of-work ×1