我编写了一个应用程序,我用它作为代理来查询数据库中的数据并自动将其加载到我的分布式Web缓存中.
我这样做是通过在配置中指定sql查询和类型.实际执行查询的代码如下所示:
List<Object> result = null;
try { result = dc.ExecuteQuery(elementType, entry.Command).OfType<Object>().ToList(); }
catch (Exception ex) { HandleException(ex, WebCacheAgentLogEvent.DatabaseExecutionError); continue; }
Run Code Online (Sandbox Code Playgroud)
elementType是从配置(使用Type.GetType())中指定的类型创建的System.Type ,并且entry.Command是SQL查询.
我遇到问题的特定实体类型如下所示:
public class FooCount
{
[Column(Name = "foo_id")]
public Int32 FooId { get; set; }
[Column(Name = "count")]
public Int32 Count { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
SQL查询如下所示:
select foo_id as foo_id, sum(count) as [count]
from foo_aggregates
group by foo_id
order by foo_id
Run Code Online (Sandbox Code Playgroud)
出于某种原因,在执行查询时,"Count"属性最终会填充,但不会填充"FooId"属性.我自己尝试运行查询,并返回正确的列名,列名与我在映射属性中指定的名称相匹配.救命!
我一直在尝试将对象映射封装在项目数据存储库中.也许EF将提供所需的抽象级别,但由于一系列原因我目前正在使用Linq to SQL.以下代码旨在将数据库中的用户作为ModUser对象列表返回,其中ModUser是存储库公开的POCO:
public List<ModUser> GetUsers() {
Users.Select(MapUser).ToList();
}
public Expression<Func<User, ModUser>> MapUser {
get {
return u => new ModUser() {
UserId = u.User_Id,
UserResources = u.Resources(MapResource)
}
}
}
public Expression<Func<Resource, ModResource>> MapResource { ...
Run Code Online (Sandbox Code Playgroud)
代码将失败,因为我无法调用MapResource表达式,因为我试图从另一个表达式中调用它.我设法通过用'=> new ModResource()替换'MapResource'来解决这个问题,然后使用ExpressionVisitor找到这个占位符节点并用MapResource表达式替换它.
当我尝试使用涉及单个属性的表达式(即UserResource = MapResource)分配ModUser的属性时,我也遇到了类似的问题.我已经设法通过使用Expression类上的方法手动组合所需的表达式来解决第二个问题.
我意识到我可以将上面的代码更改为
UserResources = u.Resources(r => MapResource.Compile().Invoke(r));
Run Code Online (Sandbox Code Playgroud)
但是生成的最终SQL查询需要获取r的所有属性,而不仅仅是MapResouce所需的属性,因为我们现在处理的是一个函数.此外,如果MapResouce需要访问r上的其他表,则不可能,因为它被用作函数而不是表达式.我可以将DeferredLoadingEnabled设置为true,但这会产生大量单个查询,而不是修改主查询以与所需的任何表连接.
有谁知道在将来的.NET版本中这些操作是否会变得更容易,或者我是否会以错误的方式解决这个问题?我非常喜欢Linq和Expression功能,我希望我可以使用更易读的代码来使用它们.
更新
以为我可能会添加一些示例,说明我如何使表达式更具组合性.他们并不简洁,但他们完成了工作.
public Expression<Func<User, ModUser>> MapUser {
get {
Expression<Func<User, ModUser>> mapUser = u => new ModUser() {
UserId = u.User_Id,
UserResources = u.Resources(r => new ModResource())
};
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用路由实现本地化
我有以下内容:
routes.MapRoute( "DefaultLocalized",
"{lang}/{controller}/{action}/{id}",
new { controller = "Home",
action = "Index",
id = "",
lang = "en" }
);
routes.MapRoute( "Default",
"{controller}/{action}/{id}",
new { controller = "Home",
action = "Index",
id = "" }
);
Run Code Online (Sandbox Code Playgroud)
当我调用我的页面时domain/en/home/index,它工作正常,但当我打电话时,domain/home/index我得到错误404:无法找到资源.
此外,当我在domain/en/home/index我点击一个安全的页面时,我被重定向到domain/Account/login如何重定向到domain/en/Account/login?
另外,当我收到应用程序错误时,如何将其重定向到domain/en/home/error?
真正的问题是如何使用语言作为路由参数实现本地化?
如何通过UDP将数据通过PHP发送到IP地址?
如何在另一台计算机上恢复该数据?
<?php
$fp = pfsockopen( "udp://192.168.1.6", 9601, $errno, $errstr );
if (!$fp)
{
echo "ERROR: $errno - $errstr<br />\n";
}
socket_set_timeout ($fp, 10);
$write = fwrite( $fp, "kik" );
//$data .= fread($fp,9600);
//echo "$data<br>";
fclose($fp);
echo "<br>Connection closed ..<br>";
if (!$write) {
echo "error writing to port: 9600.<br/>";
next;
?>
Run Code Online (Sandbox Code Playgroud)
这段代码向"kik"发送了一个我可以在另一台计算机上读取它的程序,但我怎样才能在浏览器中看到它?
我必须在我的SQL Server数据库中定期插入一些数据.但是我读取数据的源会重复之前插入的一些数据.当我使用Linq-to-SQL插入数据库时,某些数据会被复制,或者引发主键冲突异常,具体取决于主键.
如何在没有重复的情况下插入数据,没有例外?我不想用try-catch来避免异常,因为一旦引发异常,就不会插入其余的数据.
更新我也找到了自己的解决方案:我写了一个重复的条目删除存储过程,它在InsertAllOnSubmit + SubmitChanges之后运行
在Visual Studio 2008中工作(C#)...我使用List集合来存储我的自定义类(Shift)的实例.
我想使用Remove方法从列表中删除某个班次.
但List.Remove()总是删除它找到的第一个项目.
我为我的Shift实现了IComparable接口,我认为这就够了,然后我添加了IEqualityComparer的实现,它仍然没有效果.
以下是我的实施摘录:
地区IComparable会员
Run Code Online (Sandbox Code Playgroud)public int CompareTo(object obj) { Shift s1 = this; Shift s2 = (Shift)obj; if (s1.start.time != s2.start.time) return s1.start.CompareTo(s2.start); else return s1.end.CompareTo(s2.end); }endregion
region IEqualityComparer成员
Run Code Online (Sandbox Code Playgroud)public bool Equals(Shift x, Shift y) { if ((x.opening) != (y.opening)) return false; if ((x.closing) != (y.closing)) return false; if (!x.opening) if (x._start != y._start) return false; if (!x.closing) if (x._end != y._end) return false; if (x.when != y.when) return false; if (x.day != y.day) return …
我有一个父子表关系.在下面的示例中,Foo有一个FooID和一个可空的ParentFooID,它指向父记录.
Bar表始终链接到父记录.这是我用来获取结果的SQL.
Select * from Foo f
JOIN Bar b
ON b.FooID =
CASE
WHEN f.ParentFooID is null
THEN f.FooID
ELSE f.ParentFooID
END
Run Code Online (Sandbox Code Playgroud)
我在进入LINQ查询时遇到了一些麻烦.我想避免像以下那样的交叉连接:
var q = from f in Foo
from b in Bar
where b.FooID == (f.ParentFooID ?? f.FooID)
Run Code Online (Sandbox Code Playgroud)
干杯,
丹尼尔
假设我有一个强大的.NET程序集.只有我可以访问私钥.然后我将程序集分发给某个客户端系统.
客户修改程序集有多难?即:他们需要做些什么才能修改我的装配?
我的团队正在尝试将Chutzpah集成到TFS 2012构建过程中.我们以此博客文章为出发点.
在较高的层面上,实际问题是构建代理上下文中的Visual Studio Test Runner根本没有找到Chutzpah钩子.因此,虽然我们可以定义**\*.js为测试源,但实际上没有找到并初始化Chutzpah引导程序,测试运行器不会对这些文件执行任何操作.
在更详细的层面上,当我们检查日志以加载构建控制器的自定义程序集时,我们得到三条有关消息:
Summary: There were 0 failures, 2 errors and 1 warnings loading custom activities and services.
Error: Method 'ToXml' in type 'Chutzpah.VS2012.TestAdapter.ChutzpahAdapterSettings' from assembly 'Chutzpah.VS2012.TestAdapter, Version=2.2.0.171, Culture=neutral, PublicKeyToken=1ca802c37ffe1896' does not have an implementation.
Error: API restriction: The assembly '...\AppData\Local\Temp\VSTFSBuild\8c8e9402-1169-4782-99a9-ce42f83be8f0\A1288811191\Chutzpah\Microsoft.VisualStudio.TestPlatform.ObjectModel.dll' has already loaded from a different location. It cannot be loaded from a new location within the same appdomain.
Warning: Could not load file or assembly '...\AppData\Local\Temp\VSTFSBuild\8c8e9402-1169-4782-99a9-ce42f83be8f0\A1288811191\Chutzpah\phantomjs.exe' or one of its …Run Code Online (Sandbox Code Playgroud) .net ×6
c# ×5
linq-to-sql ×4
asp.net-mvc ×1
chutzpah ×1
conditional ×1
datacontext ×1
equality ×1
executequery ×1
generic-list ×1
insert ×1
join ×1
lambda ×1
linq ×1
localization ×1
php ×1
routing ×1
sockets ×1
strongname ×1
tfs ×1
tfs2012 ×1
udp ×1
yagni ×1