在演示期间,我看到了一段测试代码,开发人员在代码中粘贴了一个url.当开发人员构建应用程序时一切正常,但我们都非常好奇为什么编译器接受了url作为一行.
public class Foo
{
// Why doesn't 'http://www.foo.org' break the build?
public void Bar()
{
http://www.foo.org
Console.WriteLine("Do stuff");
}
}
Run Code Online (Sandbox Code Playgroud)
为什么上面的代码构建?编译器是否将该行视为注释?
我有这个泛型类,它使用Entity Framework 6.x.
public class GenericRepository<TEntity, TId> where TEntity, class, IIdentifyable<TId>
{
public virtual TEntity GetById(TId id)
{
using (var context = new DbContext())
{
var dbSet = context.Set<TEntity>();
var currentItem = dbSet.FirstOrDefault(x => x.Id == id);
return currentItem;
}
}
public virtual bool Exists(TId id)
{
using (var context = new DbContext())
{
var dbSet = context.Set<TEntity>();
var exists = dbSet.Any(x => x.Id == id);
return exists ;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这些接口:
public interface IIdentifyable : IIdentifyable<int>
{
} …
Run Code Online (Sandbox Code Playgroud) 我正在使用OData(WebAPI和EF)来查询数据库.现在我必须将三个表" 合并 "成一个结果.
我有3个表和一个接口,看起来像这样:
public class Authority : IAssociationEntity
{
public string Name { get; set; }
public int AuthorityId { get; set; }
}
public class Company : IAssociationEntity
{
public string Name { get; set; }
public int CompanyId { get; set; }
}
public class Organization : IAssociationEntity
{
public string Name { get; set; }
public int OrganizationId { get; set; }
}
public interface IAssociationEntity
{
string Name { get; set; }
} …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用NSubstitute来模拟替换的返回值,但我无法让替换返回正确的值,因为方法签名使用的是Func.
我已经看过这些问题了,但是不能让它与我的Func一起工作.
我试图模拟的界面是这样(有点简单):
public interface IOrgTreeRepository<out T> where T : IHierarchicalUnit
{
T FirstOrDefault(Func<T, bool> predicate);
}
Run Code Online (Sandbox Code Playgroud)
我用NSubstitute代替它:
_orgTreeRepository = Substitute.For<IOrgTreeRepository<IOrganizationUnit>>();
Run Code Online (Sandbox Code Playgroud)
然后我尝试更改返回值,如下所示:
_orgTreeRepository.FirstOrDefault(Arg.Is<Func<IOrganizationUnit, bool>>(x => x.Id== _itemsToUpdate[0].Id)).Returns(existingItems[0]);
Run Code Online (Sandbox Code Playgroud)
但它只是在existingItems中返回一个代理对象而不是我定义的对象.
但是,由于其他问题我设法让它工作,但它没有帮助我,因为我每次都需要一个特定的项目.
_orgTreeRepository.FirstOrDefault(Arg.Any<Func<IOrganizationUnit, bool>>()).Returns(existingItems[0]); // Semi-working
Run Code Online (Sandbox Code Playgroud)
我想它将lambda表达式视为一种绝对引用,因此会跳过它?有什么方法可以嘲笑返回值吗?
我正在使用Unity并尝试尽可能遵循SOLID原则.因此,所有实现仅具有接口的依赖性.
我有一个collectionwrapper,看起来像这样:
public interface ICollectionWrapper<TModel>
{
int TotalCount { get; set; }
IEnumerable<TModel> Items { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我想创建ICollectionFactory<T>
一个带工厂的实例.这是我到目前为止所得到的:
public interface ICollectionWrapperFactory
{
ICollectionWrapper<T> CreateCollection<T>();
ICollectionWrapper<T> CreateCollection<T>(IEnumerable<T> items);
ICollectionWrapper<T> CreateCollection<T>(IEnumerable<T> items, int totalCount);
}
public class CollectionWrapperFactory : ICollectionWrapperFactory
{
private readonly IUnityContainer _container;
public CollectionWrapperFactory(IUnityContainer container)
{
_container = container;
}
public ICollectionWrapper<T> CreateCollection<T>()
{
var collectionWrapper = _container.Resolve<ICollectionWrapper<T>>();
return collectionWrapper;
}
public ICollectionWrapper<T> CreateCollection<T>(IEnumerable<T> items)
{
throw new System.NotImplementedException();
}
public ICollectionWrapper<T> CreateCollection<T>(IEnumerable<T> items, int …
Run Code Online (Sandbox Code Playgroud) 我有几个数据库表,我需要确保一些列总是唯一的.我目前使用这样的唯一约束:
ALTER TABLE [dbo].[MyTable]
ADD CONSTRAINT [AK_MyTable_Unique_Cols]
UNIQUE NONCLUSTERED ([Field_1] ASC, [Field_2] ASC,
[Field_3] ASC, [FkDeliveryId] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
Run Code Online (Sandbox Code Playgroud)
表格看起来像这样.请注意,Sum不是约束的一部分.
Field_1 | Field_2 | Field_3 | FkDeliveryId | Sum
Foo | Foo | Bar | 1 | 100
Foo | Bar | Bar | 1 | 900
Bar | Foo | Foo | 1 | 400
Bar | Foo …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Visual Studio 2019,并为我的 .NET Core 项目添加了 Docker 支持。当我部署它时,一切正常,但由于域限制,我无法在本地运行 Docker。添加了 docker 支持后,有什么方法可以在本地不使用 Docker 的情况下运行/调试项目吗?
我的.csproj看起来像这样:
<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
....
Run Code Online (Sandbox Code Playgroud)
还有我的launchSettings.json:
{
"profiles": {
"SomeProject": {
"commandName": "Project"
},
"Docker": {
"commandName": "Docker"
}
}
}
Run Code Online (Sandbox Code Playgroud) 是否可以绑定ngOptions
到$scope
?之外的值?
我有一组枚举,将自动呈现为javascript.这些目前不是"角度域"的一部分,但我想将其绑定ngOptions
到其中一个数组,我不想手动将项目复制到范围中.
我想要这个的原因是因为我有一些自动渲染项目的HTML Helpers,所以我想要一个非常通用的解决方案而不需要向控制器添加大量代码.那可能吗?
var NS = NS || {};
NS.Sub = NS.Sub || {};
// This is auto-generated:
NS.Sub.enums = {"deliveryStatus":[{"id":1,"label":"Delivered"},{"id":2,"label":"Expected"},{"id":4,"label":"Failed"}],"documentType":[{"id":0,"label":"Pdf"},{"id":1,"label":"Rtf"}]};
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
// If I copy the items to the scope it works.
$scope.items = NS.Sub.enums.deliveryStatus;
$scope.model = {}
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<div ng-controller="MyCtrl">
<p>Not working SELECT (bound to array outside of scope)</p>
<select ng-model="model.status" ng-options="item.label for item in NS.Sub.enums.deliveryStatus"></select>
<p>Working SELECT (bound to array …
Run Code Online (Sandbox Code Playgroud)我将 OData v4 与 WebAPI 与这些 nuget 包一起使用:
Microsoft.AspNet.WebApi.OData 4.0.30506
我无法groupby
与计数一起工作。这应该是最简单的事情。
我有一个简单的表/实体,名为Delivery
. 它有一个状态列(在其他几个列中)。我基本上想做的是这个 SQL 查询,但是使用 OData:
SELECT e.status, count(*) AS total
FROM Delivery e
GROUP BY e.status
Run Code Online (Sandbox Code Playgroud)
我试过使用countdistinct
,但它没有给我正确的结果。
/odata/deliveries?$apply=groupby((status),aggregate(status with countdistinct as total))
Run Code Online (Sandbox Code Playgroud)
它返回:
"value": [
{
"@odata.id": null,
"total": 1,
"status": 1
},
{
"@odata.id": null,
"status": 2,
"total": 1
},
{
"@odata.id": null,
"status": 4,
"total": 1
}
]
Run Code Online (Sandbox Code Playgroud)
正确的结果应该是(这是我的 SQL 查询返回的):
status total
1 2
2 22
4 1 …
Run Code Online (Sandbox Code Playgroud) 嗨,我是新的连接到服务器/数据库,并想知道为什么这会返回一个错误.
我有一个带数据库的FastHost服务器.
我刚刚输入了一个IP示例,但我一直在使用网站控制面板上给出的IP.
private void SQLTest_Click(object sender, RoutedEventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source = 123.456.789.012" +
"Initial Catalog = DiscoverThePlanet" +
"User ID = TestUser" +
"Password = Test";
try
{
conn.Open();
MessageBox.Show("Connection Established!");
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open Connection!");
}
}
Run Code Online (Sandbox Code Playgroud)
这会返回
无法打开Connection!"消息.
我在我的代码中得到以下显示:'System.Data.SqlClient.SqlException'
System.Data.dll中出现类型异常,但未在用户代码中处理
附加信息:建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确,以及SQL Server是否配置为允许远程连接.(提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)
我知道我的服务器很好,因为我在SQL Server Management studio上连接了它并添加了表和数据.
c# ×7
sql ×2
sql-server ×2
angularjs ×1
constraints ×1
docker ×1
generics ×1
iqueryable ×1
javascript ×1
linq ×1
nsubstitute ×1
odata ×1
unit-testing ×1