我正在尝试构建类似条件查询的东西,以便从底层数据库中获取所需的数据.
目前我有以下查询(工作正常)
var eventData = dbContext.Event.Select(t => new
{
Address = true ? new AnonymousEventGetAddress
{
AddressLine1 = t.Address.AddressLine1,
CityName = t.Address.AddressCityName
} : new AnonymousEventGetAddress(),
});
Run Code Online (Sandbox Code Playgroud)
如果我改成它
var includeAddress = true; // this will normally be passed as param
var eventData = dbContext.Event.Select(t => new
{
Address = includeAddress ? new AnonymousEventGetAddress
{
AddressLine1 = t.Address.AddressLine1,
CityName = t.Address.AddressCityName
} : new AnonymousEventGetAddress(),
});
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
"AnonymousEventGetAddress"类型出现在单个LINQ to Entities查询中的两个结构不兼容的初始化中.A型可以在同一个查询两个地方被初始化,但只有当同一属性在两个地方设置和这些属性以相同的顺序设置.
我在这里做错了什么(因为true它正在工作)以及如何解决这个问题?
我知道将else-part改为
new AnonymousEventGetAddress
{
AddressLine1 = …Run Code Online (Sandbox Code Playgroud) 鉴于以下内容class:
public class DataPair{
public string Key { get; set; }
public object Value { get; set; }
public DataPair(string key, object value)
{
Key = key;
Value = value;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有机会实现类似的东西
public static implicit operator DataPair(dynamic value)
{
return new DataPair(value.Key, value.Value);
}
Run Code Online (Sandbox Code Playgroud)
所以我可以用这种方式创建一个新实例
DataPair myInstance = {"key", "value"};
Run Code Online (Sandbox Code Playgroud) 我正在尝试将对象数组转换为哈希图。我只有 ES6 的某些部分可用,我也不能使用Map。
数组中的对象非常简单,例如{nation: {name: string, iso: string, scoringPoints: number}. 我需要对它们进行排序scoringPoints。我现在想要一个按 iso -> 排序的“字典” {[iso:string]:number}。
我已经尝试过(从这里 (SO))
const dict = sortedData.reduce((prev, curr, index, array) => (
{ ...array, [curr.nation.iso]: ++index }
), {});
Run Code Online (Sandbox Code Playgroud)
但dict结果是一个Object以0.开头的索引。希望只是我没有看到的一件小事。但目前我的脑子在思考如何将一个简单的数组转换成一个类似 hashmap 的对象。也许Array.map?
我还应该注意,我正在使用TypeScript它之前我也遇到了一些问题,因为输入不正确。
const test = [
{ nation: { name: "Germany", iso: "DE", rankingPoints: 293949 } },
{ nation: { name: "Hungary", iso: "HU", rankingPoints: 564161 …Run Code Online (Sandbox Code Playgroud) 我有我的本地SQLite数据库有以下问题.我正在尝试使用在线SQL-Server数据库更新通过同步过程存储的数据.虽然同步化没有问题,但在尝试更新本地值时经常会遇到错误.例如,每个表上可能存在违反现有Primary-Key-Constraints的情况.另一方面,在具体的桌子上也可能违反了Unique-Key.
我的问题是如何找出发生了哪种类型的错误.
我抓住数据库更新或插入之类的
catch(UpdateException ue)
{
HandleUpdateException(ue);
}
Run Code Online (Sandbox Code Playgroud)
handle方法如下:
private void HandleUpdateException(UpdateException e)
{
// get inner exception as SQLiteException
var innerException = e.InnerException as SQLiteException;
if(innerException != null)
{
switch(innerException.ErrorCode)
{
case SQLiteError.Constraint:
// handle constraint-error here
break;
default:
// log error
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我现在该怎么发生错误?重要的是要知道,因为PK违规的处理完全不是违反英国的行为.
我可以试着做
if(e.Message.Contains("unique")) // ...
Run Code Online (Sandbox Code Playgroud)
要么
if(e.Message.Contains("foreign")) // ...
Run Code Online (Sandbox Code Playgroud)
但我不喜欢用"魔法"字符串检查这种错误.
所以任何帮助将不胜感激.
我是新来的C#小号await/async,目前玩弄了一下.
在我的场景中,我有一个简单的客户端对象,它有一个WebRequest属性.客户端应该通过WebRequests 定期发送活动消息RequestStream.这是client-object的构造函数:
public Client()
{
_webRequest = WebRequest.Create("some url");
_webRequest.Method = "POST";
IsRunning = true;
// --> how to start the 'async' method (see below)
}
Run Code Online (Sandbox Code Playgroud)
和async alive-sender方法
private async void SendAliveMessageAsync()
{
const string keepAliveMessage = "{\"message\": {\"type\": \"keepalive\"}}";
var seconds = 0;
while (IsRunning)
{
if (seconds % 10 == 0)
{
await new StreamWriter(_webRequest.GetRequestStream()).WriteLineAsync(keepAliveMessage);
}
await Task.Delay(1000);
seconds++;
}
}
Run Code Online (Sandbox Code Playgroud)
该方法应该如何开始?
new Thread(SendAliveMessageAsync).Start();
要么
Task.Run(SendAliveMessageAsync); //将返回类型更改为Task …
我正在尝试动态构建一些sql查询,具体取决于给定的配置,只查询所需的数据:
在编写简单的linq时,它看起来像这样:
var data = dbContext
.TableOne
.Select(t1 => new TableOneSelect
{
TableOneId = t1.TableOneId,
TableOneTableTwoReference = new[] { TableOne.FirstTableTwoReference.Invoke(t1) }
.Select(t2 => new TableTwoSelect
{
TableTowId = (Guid?)t2.TableTwoId,
// ... some more properties of t2
}).FirstOrDefault(),
// ... some more properties of t1
});
Run Code Online (Sandbox Code Playgroud)
而TableOne.FirstTableTwoReference.Invoke(t1)定义
public static Expression<Func<TableOne, TableTwo>> FirstTableTwoReference => (t1) => t1.TableTwoReferences.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
目前,我有以下动态构建TableOne-part:
public Expression<Func<TableOne, TableOneSelect>> Init(TableOneConfig cfg)
{
var memberBindings = new List<MemberBinding>();
var selectType = typeof(TableOneSelect);
var newExpression = Expression.New(selectType);
var theEntity = …Run Code Online (Sandbox Code Playgroud) 我在使用正确的继承链时遇到了问题,而没有丢失DRY原则的优点.
鉴于以下结构:
现在,我需要有两个共享相同功能的新类.但是一个是A类,而另一个是B类.(这不能改变!)两个类都应该有一个方法SetSize().
所以最后会有C类:A和D:B,两者都有相同的SetSize方法.
问题:如何创建基类中间层以使SetSize()-method仅声明/实现一次(DRY)?我想有关使用接口或一些静态助手类来实现逻辑的东西SetSize()?
是否有任何模式或最佳实践来实现此行为?
在我的MVC项目中,我有不同的自定义验证属性.其中之一是检查属性的值与另一个属性的值.
正如许多文章中所述,我添加了类似的内容
result.ValidationParameters.Add("otherproperty", _otherPropertyHtml);
result.ValidationParameters.Add("comparetype", _compareType);
result.ValidationParameters.Add("equalitytype", _equalityType);
Run Code Online (Sandbox Code Playgroud)
返回的ModelClientValidationRule对象.
我现在的问题是,如果我要检查的属性被封装在另一个对象中,验证将无效.
如果我创造类似的东西
@Html.TextBoxFor(m => m.ValueOne)
@Html.TextBoxFor(m => m.ValueTwo)
Run Code Online (Sandbox Code Playgroud)
验证将在呈现时正常工作
data-val-otherproperty="ValueTwo"
Run Code Online (Sandbox Code Playgroud)
我的问题是以下几点
@Html.TextBoxFor(m => m.IntermediateObject.ValueOne)
@Html.TextBoxFor(m => m.IntermediateObject.ValueTwo)
Run Code Online (Sandbox Code Playgroud)
这将呈现两个带有名称IntermediateObject_ValueOne和的文本框IntermediateObject.ValueTwo.但仍然是第一个文本框的data-val-otherproperty ="ValueOne".
如何才能实现,这data-val-otherproperty一直是其他财产的正确名称?
我的想法类似于HtmlHelper <>.NameFor(m => ...)或使用反射的东西?
更新1 - 根据评论的要求添加代码
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = false)]
public class CustomCompareToOther : ValidationAttribute, IClientValidatable
{
// private backing-field
private readonly string _otherPropertyName;
// constructor
public OemCompareToOther(string otherPropertyName)
{
_otherPropertyName = otherPropertyName;
}
// implementation of IClientValidatable
public IEnumerable<ModelClientValidationRule> …Run Code Online (Sandbox Code Playgroud) 使用bootstraps模式的示例
<div class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Run Code Online (Sandbox Code Playgroud)
我想知道是否有办法实现这一点,某种动态通道jQuery.
我的目标是有一个类似变量的东西$theModal,它将被初始化并具有获取/设置标题的属性,内容,当点击保存/取消/关闭时要调用的javascipt函数等.
是应该通过jQuery代码生成还是应该在代码中生成标记并使用ids/custom data-attributes来捕获模态?
也许是一些阶级结构?
var $theModal = new MyModal();
Run Code Online (Sandbox Code Playgroud)
接下来的问题是,如果模态已经打开,如何创建克隆?
我想做/猜
var $theClone = $theModal.clone().init();
$theClone.title = "Title of the second modal";
$theClone.content = $.ajax(url); …Run Code Online (Sandbox Code Playgroud) javascript jquery modal-dialog twitter-bootstrap bootstrap-modal
我怎样才能告诉我的mvc-application 路由到特定的Controller以及Action何时未指定?
调试http://localhost:54500/时应路由到http://localhost:54500/Home/Index.
目前我有:
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
但这总是抛出
未找到视图“索引”或其主视图或没有视图引擎支持搜索的位置
编辑 #1
它应该重定向/路由到驻留在Area被调用的视图中Home。只是想澄清一下,有 aController和 anArea两者都被命名为Home。
该区域的配置是:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
Run Code Online (Sandbox Code Playgroud) c# ×7
asp.net-mvc ×2
javascript ×2
asp.net ×1
async-await ×1
asynchronous ×1
constructor ×1
database ×1
dry ×1
dynamic ×1
expression ×1
inheritance ×1
jquery ×1
linq ×1
modal-dialog ×1
reflection ×1
routes ×1
sqlite ×1
try-catch ×1
typescript ×1
url-routing ×1
validation ×1
webrequest ×1