我刚刚发现Entity Framework中的延迟加载只能从创建它的线程中运行ObjectContext.为了说明这个问题,我做了一个简单的测试,只有一个包含2个实体的简单模型:Person和Address.这是代码:
private static void TestSingleThread()
{
using (var context = new TestDBContext())
{
foreach (var p in context.Person)
{
Console.WriteLine("{0} lives in {1}.", p.Name, p.Address.City);
}
}
}
private static void TestMultiThread()
{
using (var context = new TestDBContext())
{
foreach (var p in context.Person)
{
Person p2 = p; // to avoid capturing the loop variable
ThreadPool.QueueUserWorkItem(
arg =>
{
Console.WriteLine("{0} lives in {1}.", p2.Name, p2.Address.City);
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
该TestSingleThread …
我正在尝试实现泛型方法,该方法用于将Tuple<Descendant>类型的对象转换为类型的对象Tuple<Ancestor>.我遇到了一个似乎是C#语言限制的问题.
using System;
namespace Samples
{
public static class TupleExtensions
{
public static Tuple<SuperOfT1> ToSuper<T1, SuperOfT1>(this Tuple<T1> target)
where T1 : SuperOfT1
{
return new Tuple<SuperOfT1>(target.Item1);
}
}
public interface Interface { }
public class Class : Interface { }
static class Program
{
static void Main()
{
var tupleWithClass = new Tuple<Class>(new Class());
// Next instruction lead the compilation to error. :( The compiler doesn't try to infer types if at least one of generic …Run Code Online (Sandbox Code Playgroud) 对不起,如果标题不是很清楚,我想不出更好的......
我正在以a的形式接收用户输入IObservable<char>,并且我想IObservable<char[]>通过在每次用户停止输入超过1秒时对字符进行分组来将其转换为a .因此,例如,如果输入如下:
h
e
l
l
o
(pause)
w
o
r
l
d
(pause)
!
(pause)
Run Code Online (Sandbox Code Playgroud)
我希望输出可观察到:
['h', 'e', 'l', 'l', 'o']
['w', 'o', 'r', 'l', 'd']
['!']
Run Code Online (Sandbox Code Playgroud)
我怀疑该解决方案是相当简单的,但我不能找到正确的方法......我试图用Buffer,GroupByUntil,Throttle和其他几个人,但没有成功.
任何想法都会受到欢迎!
编辑:我有一些几乎可行的东西:
_input.Buffer(() => _input.Delay(TimeSpan.FromSeconds(1)))
.ObserveOnDispatcher()
.Subscribe(OnCompleteInput);
Run Code Online (Sandbox Code Playgroud)
但是每次键入新字符时我都需要重置延迟...
我每天都收到"Jenkins build is unstable"的电子邮件,用于我不再处理的项目.
我可以从构建结果(build.xml)中看到我在"罪魁祸首"列表中,因为我前一段时间提交了一个提交,并且由于构建已经不稳定几周,所以罪魁祸首列表中没有被清除了.
由于我不再从事这个项目,我想停止接收这个Jenkins工作的电子邮件通知.如何从收件人中删除自己?我应该编辑上一次构建的build.xml吗?(编辑:显然这种方法不起作用)
我如何实现IEqualityComparer<DataRow>从DataTable下一个结构中删除重复行:
ID primary key, col_1, col_2, col_3, col_4
Run Code Online (Sandbox Code Playgroud)
默认比较器不起作用,因为每行都有自己唯一的主键.
如何实现IEqualityComparer<DataRow>将跳过主键并仅比较剩余的数据.
我有这样的事情:
public class DataRowComparer : IEqualityComparer<DataRow>
{
public bool Equals(DataRow x, DataRow y)
{
return
x.ItemArray.Except(new object[] { x[x.Table.PrimaryKey[0].ColumnName] }) ==
y.ItemArray.Except(new object[] { y[y.Table.PrimaryKey[0].ColumnName] });
}
public int GetHashCode(DataRow obj)
{
return obj.ToString().GetHashCode();
}
}
Run Code Online (Sandbox Code Playgroud)
和
public static DataTable RemoveDuplicates(this DataTable table)
{
return
(table.Rows.Count > 0) ?
table.AsEnumerable().Distinct(new DataRowComparer()).CopyToDataTable() :
table;
}
Run Code Online (Sandbox Code Playgroud)
但它只打电话GetHashCode()而不打电话Equals()
在我正在开发的应用程序中,我们使用DevExpress XtraGrid控件,该控件具有RowCellStyle允许自定义每个单元格样式的事件.此事件的事件处理程序通常如下所示:
private gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
{
if (/* Some condition */)
{
e.Appearance.Font = new Font(gridView1.Appearance.Font, FontStyle.Bold);
}
}
Run Code Online (Sandbox Code Playgroud)
每次渲染单元格时都会调用此处理程序,因此它可以创建大量Font实例.所以我想知道这样做的成本......我做了一些实验,似乎每次都会创建一个新的HFONT手柄.我应该担心吗?对资源使用的影响有多大?
如果它对性能产生重大影响,是否应该有FontCache类或类似的东西?
注意:我知道如何解决问题(我只需要创建一次字体并每次都重复使用),我的问题是关于创建许多HFONT句柄的成本
我注意到泛型的一个非常奇怪的重载解决问题......
请考虑以下方法:
static void Foo<TSource>(TSource element, Func<TSource, int> selector)
{
"int".Dump();
}
static void Foo<TSource>(TSource element, Func<TSource, double> selector)
{
"double".Dump();
}
static T Identity<T>(T value)
{
return value;
}
Run Code Online (Sandbox Code Playgroud)
(C#4,在LINQPad中测试)
如果我尝试Foo使用lambda表达式作为选择器调用,一切正常:
Foo(42, x => x); // prints "int"
Run Code Online (Sandbox Code Playgroud)
但是,如果我替换x => x为Identity,编译器无法在2个Foo重载之间做出决定:
Foo(42, Identity);
// The call is ambiguous between the following methods or properties:
// 'UserQuery.Foo<int>(int, System.Func<int,int>)' and
// 'UserQuery.Foo<int>(int, System.Func<int,double>)'
Run Code Online (Sandbox Code Playgroud)
如何将第二次重载作为有效候选者?类型推断正确判断TSource是int …
我正在尝试使用SearchBoxWindows 8.1中引入的控件,但我无法弄清楚如何在结果建议中显示图像.建议出现,但图像应该保留为空白:

这是我的XAML:
<SearchBox SuggestionsRequested="SearchBox_SuggestionsRequested" />
Run Code Online (Sandbox Code Playgroud)
我的代码背后:
private async void SearchBox_SuggestionsRequested(SearchBox sender, SearchBoxSuggestionsRequestedEventArgs args)
{
var deferral = args.Request.GetDeferral();
try
{
var imageUri = new Uri("ms-appx:///test.png");
var imageRef = await StorageFile.GetFileFromApplicationUriAsync(imageUri);
args.Request.SearchSuggestionCollection.AppendQuerySuggestion("test");
args.Request.SearchSuggestionCollection.AppendSearchSeparator("Foo Bar");
args.Request.SearchSuggestionCollection.AppendResultSuggestion("foo", "Details", "foo", imageRef, "Result");
args.Request.SearchSuggestionCollection.AppendResultSuggestion("bar", "Details", "bar", imageRef, "Result");
args.Request.SearchSuggestionCollection.AppendResultSuggestion("baz", "Details", "baz", imageRef, "Result");
}
finally
{
deferral.Complete();
}
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
一些额外的细节:
我尝试用XAML Spy调试它; 每个建议ListViewItem都Content设置为一个实例Windows.ApplicationModel.Search.Core.SearchSuggestion.在这些SearchSuggestion对象,我注意到Text,Tag,DetailText,和ImageAlternateText属性设置为自己的正确的值,但Image属性为空... …
我正在尝试从一个针对多个框架(WPF,WinRT,UWP等)的库中的代码创建一个绑定,而我正在打砖墙.我想要绑定的属性是一个自定义附加属性.在WPF中,我可以将DependencyProperty自身作为绑定路径传递:
new PropertyPath(MyClass.MyAttachedProperty)
Run Code Online (Sandbox Code Playgroud)
但是在WinRT中,PropertyPath类只接受一个字符串.我试图传递属性的名称,如下所示:
new PropertyPath("(MyClass.MyAttachedProperty)")
Run Code Online (Sandbox Code Playgroud)
但当然它不起作用,因为我的类不在默认命名空间中.在XAML中,我可以将命名空间映射到前缀并使用该前缀,但据我所知,不可能从代码中执行此操作.
有没有办法在代码中创建这个绑定?
我有一个自定义泛型类型,大致如下所示:
public struct Foo<T>
{
public int Value { get; }
public string Signature { get; }
public Type Type { get; }
}
Run Code Online (Sandbox Code Playgroud)
该类型用于请求和响应主体以及控制器操作参数。一切都经过配置,以便将其序列化为字符串,并且它可以与模型绑定和 JSON 序列化配合良好。该类型有一个TypeConverter与之关联的对象,它负责将其与字符串相互转换。
然而,Swagger 架构仍然将其表示为具有 3 个属性的对象。该Type属性也被扩展,它拉出System.Reflection直接或间接暴露的所有类型Type。
如何避免这种情况并将我的类型公开为字符串?
尝试的第一个解决方案:使用MapType
我尝试使用MapType;如果我指定泛型类型参数,它可以正常工作,但不适用于开放泛型类型:
c.MapType(typeof(Foo<Something>), () => new OpenApiSchema { Type = "string" }); // Works
c.MapType(typeof(Foo<>), () => new OpenApiSchema { Type = "string" }); // Doesn't work
Run Code Online (Sandbox Code Playgroud)
如何将映射应用于Foo<T>, 对于任何T?
目前的解决方法
到目前为止,我唯一的解决方法非常丑陋:
class SchemaFilter …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×2
generics ×2
xaml ×2
asp.net-core ×1
binding ×1
datatable ×1
email ×1
fonts ×1
jenkins ×1
lazy-loading ×1
linq ×1
search-box ×1
swagger ×1
swashbuckle ×1
uwp ×1
windows-8.1 ×1