from i in Db.Items
select new VotedItem
{
ItemId = i.ItemId,
Points = (from v in Db.Votes
where b.ItemId == v.ItemId
select v.Points).Sum()
}
Run Code Online (Sandbox Code Playgroud)
我得到了这个查询,但是如果没有找到异常的投票则它会失败:
The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.
Run Code Online (Sandbox Code Playgroud)
我假设它因为sum返回一个int而不是一个可空的int,给一个int一个?作为输入只给出相同的错误,可能导致sum仅适用于int.
有什么好的解决方法吗?
我正在制作一个简单的下载服务,以便用户可以从外部网站下载他的所有图像.要做到这一点,我只需将所有内容压缩到http流.
然而,似乎一切都存储在内存中,并且直到zip文件完成并且输出关闭才发送数据.我希望服务立即开始发送,而不是使用太多内存.
public void ProcessRequest(HttpContext context)
{
List<string> fileNames = GetFileNames();
context.Response.ContentType = "application/x-zip-compressed";
context.Response.AppendHeader("content-disposition", "attachment; filename=files.zip");
context.Response.ContentEncoding = Encoding.Default;
context.Response.Charset = "";
byte[] buffer = new byte[1024 * 8];
using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipOutput = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(context.Response.OutputStream))
{
foreach (string fileName in fileNames)
{
ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(fileName);
zipOutput.PutNextEntry(zipEntry);
using (var fread = System.IO.File.OpenRead(fileName))
{
ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(fread, zipOutput, buffer);
}
}
zipOutput.Finish();
}
context.Response.Flush();
context.Response.End();
}
Run Code Online (Sandbox Code Playgroud)
我可以看到工作进程内存在生成文件时增长,然后在完成发送时释放内存.如何在不使用太多内存的情况下执行此操作?
我有这样的界面
public interface IWriter
{
...
}
Run Code Online (Sandbox Code Playgroud)
和一个班级
public class WriterMerger
{
public WriterMerger(IEnumerable<IWriter> writers)
...
}
Run Code Online (Sandbox Code Playgroud)
我希望structuremap用WriterMerger填充所有已注册的IWriter的构造函数参数.我注册了不同的作家
StructureMap.ObjectFactory.Initialize(x =>
{
x.ForRequestedType<IWriter>().AddConcreteType<MemoryWriter>();
x.ForRequestedType<IWriter>().AddConcreteType<FlatFileWriter>();
x.ForRequestedType<IWriter>().AddConcreteType<DbWriter>();
}
Run Code Online (Sandbox Code Playgroud)
但是打电话
ObjectFactory.CreateInstance<WriterMerger>();
Run Code Online (Sandbox Code Playgroud)
返回异常"没有为PluginFamily System.Collections.Generic.IEnumerable`1 [[IWriter ..]]"定义的默认实例(已编辑类名)
自动执行此操作的任何技巧?或者我是否必须制作自定义工厂方法?
protected static new void WhyIsThisValidCode()
{
}
Run Code Online (Sandbox Code Playgroud)
为什么允许覆盖静态方法?除了bug之外什么也没有,它根本不会像你想象的那样起作用.
参加以下课程.
class BaseLogger
{
protected static string LogName { get { return null; } }
public static void Log(string message) { Logger.Log(message, LogName); }
}
class SpecificLogger : BaseLogger
{
protected static string LogName { get { return "Specific"; } }
}
Run Code Online (Sandbox Code Playgroud)
这是和,代码
SpecificLogger.Log("test");
Run Code Online (Sandbox Code Playgroud)
是的,但它不会通过查看代码来实现您的想法.
它打电话Logger.Log给LogName = null.
那为什么允许这样做?
在使用自定义集合对象时,我经常发现linq存在问题.他们经常被打败
基础集合
abstract class BaseCollection<T> : List<T> { ... }
Run Code Online (Sandbox Code Playgroud)
集合定义为
class PruductCollection : BaseCollection<Product> { ... }
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法将linq exssion的结果添加到此集合而不是addrange或concat?
var products = from p in HugeProductCollection
where p.Vendor = currentVendor
select p;
PruductCollection objVendorProducts = new PruductCollection();
objVendorProducts.AddRange(products);
Run Code Online (Sandbox Code Playgroud)
如果从linq查询返回的对象属于我的自定义集合类型,那将是很好的.因为您似乎需要两次枚举该集合才能执行此操作.
编辑:阅读答案后,我认为最好的解决方案是实现ToProduct()扩展.想知道c#4.0中的协方差/逆变是否有助于解决这些问题.
我有一个像这样的ASP.NET MVC控制器
[Authorize]
public class ObjectController : Controller
{
public ObjectController(IDataService dataService)
{
DataService = dataService;
}
public IDataService DataService { get;set;}
}
Run Code Online (Sandbox Code Playgroud)
Authorize属性在框架中定义为"Inherited = true".所以当我制作下一个控制器时:
public class DemoObjectController : ObjectController
{
public DemoObjectController(IDataService dataService)
: base (dataService)
{
DataService = new DemoDataService(DataService);
}
}
Run Code Online (Sandbox Code Playgroud)
它获取authorize属性,但我不想在这里.我希望Demo Object控制器可供所有人使用,因为它只使用假数据.
我想我将实现自己的Authorize属性,它不会被继承,因为我找不到任何方法从继承的类中删除属性.
随着 NHibernate 3.0 版本的临近,它到底有什么变化和新内容?
有什么好的资源可以查找更改吗?
c# ×5
.net ×2
linq ×2
asp.net ×1
attributes ×1
http ×1
inheritance ×1
linq-to-sql ×1
nhibernate ×1
nullable ×1
static ×1
stream ×1
structuremap ×1
sum ×1
zip ×1