如何在属性中使用静态Guid作为参数?
static class X
{
public static readonly Guid XyId = new Guid("---");
}
[MyAttribute(X.XyId)] // does not work
public class myClass
{
}
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为Guid必须是readonly,它不能是const.string和byte []表示也是readonly.
这有什么解决方法吗?
我如何转换/转换列包含字符串,例如Jan.08,Feb.08 ..进入日期格式,以便我可以对它们进行排序?
非常感谢!
我为我的项目方法创建了一个单元测试.当找不到文件时,该方法引发异常.我为此编写了一个单元测试,但是在引发异常时我仍然无法通过测试.
方法是
public string[] GetBuildMachineNames(string path)
{
string[] machineNames = null;
XDocument doc = XDocument.Load(path);
foreach (XElement child in doc.Root.Elements("buildMachines"))
{
int i = 0;
XAttribute attribute = child.Attribute("machine");
machineNames[i] = attribute.Value;
}
return machineNames;
}
Run Code Online (Sandbox Code Playgroud)
单元测试
[TestMethod]
[DeploymentItem("TestData\\BuildMachineNoNames.xml")]
[ExpectedException(typeof(FileNotFoundException),"Raise exception when file not found")]
public void VerifyBuildMachineNamesIfFileNotPresent()
{
var configReaderNoFile = new ConfigReader();
var names = configReaderNoFile.GetBuildMachineNames("BuildMachineNoNames.xml");
}
Run Code Online (Sandbox Code Playgroud)
我应该处理方法中的异常还是我错过了其他的东西?
编辑:
我传递的路径不是找到文件的路径,所以这个测试应该通过...即如果文件不存在于该路径中该怎么办.
我获取具有相同ID的多个记录,我想将它们存储Hashtable在C#中.我使用id作为键中的键,Hashtable值是对象本身.它会引发异常,因为会再次添加相同的密钥.有没有办法解决这个问题?
这是我的代码:
Hashtable localItemsIndex = new Hashtable();
foreach (TzThing thing in localThingz)
localItemsIndex.Add(thing.Id, thing);
Run Code Online (Sandbox Code Playgroud)
在此先感谢jennie
我有两张桌子,'玩家'和'物品'.玩家有一个项目列表.我想使用分页来检索玩家及其所有物品.我想基于玩家分页,而不考虑有多少项目.
所以我这样做:
Criteria c = session.createCriteria(Players.class).setFetchMode("items", FetchMode.JOIN);
c.addOrder(Order.asc("playerID"));
c.setFirstResult(0);
c.setMaxResults(25);
List<Player> players = c.list();
Run Code Online (Sandbox Code Playgroud)
这会给我前25名球员,还是会给我前25项(按球员分组)?我想知道这种行为是否像JPA查询一样未定义,或者是否有保证答案.
无论如何,什么是Criteria查询会给我前25名球员或前25名球员项目组合(按球员ID排序,然后是项目ID)?
如何在这个类的每个子类策略的新NHibernate Mapping-By-Code中编写映射:
public class Person
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class JuridicalPerson : Person
{
public virtual int Id { get; set; }
public virtual string LegalName { get; set; }
}
public class PrivatePerson : Person
{
public virtual int Id { get; set; }
public virtual bool Sex { get; set; }
}
Run Code Online (Sandbox Code Playgroud) nhibernate inheritance nhibernate-mapping nhibernate-mapping-by-code
我必须将尾随零添加到十进制值.不仅用于显示(因此Format不是选项),而是用于实际的底层数据,因为小数精度在我们的应用程序中很重要.
我试过了:
decimal value = 1M
decimal withPrecision = value + 0.000M;
Run Code Online (Sandbox Code Playgroud)
在许多情况下哪种方法效果很好......奇怪的是并非总体而言.我调试了一个情况,其中withPrecision中的值仍然是1M,没有看到运行时的值和立即窗口中相同的硬编码值有任何差异.我还使用了decimal.GetBits来找到差异 - 没有.
我试过(这里提出调整小数精度,.net):
decimal value = 1M
decimal withPrecision = value * 1.000M;
Run Code Online (Sandbox Code Playgroud)
效果很好 - 除了案例值为零.然后结果为0M,没有任何尾随零.我也不相信解决方案,在其他情况下也可能不起作用.
目前我在:
decimal value = 1M
decimal withPrecision = (value * 1.000M) + 0.000M;
Run Code Online (Sandbox Code Playgroud)
这在我目前发现的所有情况下都有效......但是看起来也不值得信赖.我还可以为零实现一个例外情况.
我认为Format并且Parse会工作.我不喜欢它.它看起来不是很快,我不明白为什么我必须将小数字放入一个字符串只是为了操纵它.
我开始相信这样一个简单的任务没有干净的解决方案.
我使用 dapper 将存储过程中的结果集获取到对象列表中,并将其作为 json 返回给客户端:
public IHttpActionResult Test()
{
List<ProductPreview> gridLines;
var cs = ConfigurationManager.ConnectionStrings["eordConnection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(cs))
{
gridLines = conn.Query<ProductPreview>("dbo.myStoredProcedure", new { userID = 1 },
commandType: CommandType.StoredProcedure).ToList();
}
var totalCount = gridLines[0].MaxCount;//I need to know total count
....
return Ok(gridLines);
}
Run Code Online (Sandbox Code Playgroud)
有用。ProductPreview 类型的对象的最后一个属性是 TotalCount,因为存储过程将总计数作为每行的列返回。(第二个选项是存储过程返回两个记录集,但我不确定如何更改 dapper 以使用两个记录集)。不能选择进行两个单独的查询。
在没有 TotalCount 属性的情况下将 gridLines json 对象返回给客户端(因为它是开销)并将总计数从存储过程读取到某个变量的最佳方法是什么?将 gridLines 对象复制到没有 TotalCount 属性的其他对象也会产生不必要的开销。
我有以下简单表来重现该问题:
<TABLE>
<TR>
<TD style="border: black solid 1px; width:24px; height:68px; margin:0px; padding:0px" >
<IMG
style="width: 24px; height: 68px; margin:0px; padding:0px; border:none"
src="Image24x68.png">
</TD>
</TR>
</TABLE>
Run Code Online (Sandbox Code Playgroud)
图像实际上是24x86像素大.边界只是标记细胞的边界.没有为文档分配css文件.
我希望单元格与图像一样大.
问题是:在任何IE版本(6,7,8)中,表格单元格总是呈现几个像素太高,而在Firefox和其他浏览器中它可以正常工作.
这有什么解决方案/解决方法吗?
为什么以下HQL查询失败?
string hql = @"delete MyLog log
where
log.UtcTimestamp < :threshold and
log.Configuration.Application = :application";
session.CreateQuery(hql)
.SetDateTime("threshold", threshold)
.SetEnum("application", this.application)
.ExecuteUpdate();
Run Code Online (Sandbox Code Playgroud)
在select中使用相同形式的查询:
string hql = @"from MyLog log
where
log.UtcTimestamp < :threshold and
log.Configuration.Application = :application";
IList<MyLog> log = session.CreateQuery(hql)
.SetDateTime("threshold", threshold)
.SetEnum("application", this.application)
.List<MyLog>();
Run Code Online (Sandbox Code Playgroud)
MyLog的映射包含:
References(x => x.Configuration)
.Columns("CONFIGURATION_ID")
.ReadOnly();
Run Code Online (Sandbox Code Playgroud)
Configuration的映射包含:
Map(x => x.Application, "APPLICATION_ID");
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
从MYLOG中删除,CONFIGURATION countercon1_,其中UTC_TIMESTAMP <:p0和APPLICATION_ID =:p1; :p0 = 04/10/2010 17:15:52,:p1 = 7
NHibernate.Exceptions.GenericADOException:无法执行更新查询[SQL:
从MYLOG,CONFIGURATION中删除countercon1_,其中UTC_TIMESTAMP <?和APPLICATION_ID =?
] ---> Oracle.DataAccess.Client.OracleException:ORA-00933:SQL命令未正确结束
c# ×5
nhibernate ×2
attributes ×1
const ×1
css ×1
dapper ×1
date ×1
decimal ×1
digits ×1
guid ×1
hashtable ×1
hibernate ×1
hql ×1
hql-delete ×1
html ×1
html-table ×1
image ×1
inheritance ×1
ora-00933 ×1
precision ×1
sql ×1
sql-server ×1
string ×1
unit-testing ×1
webapi2 ×1