相关疑难解决方法(0)

在实体框架代码第一种方法中映射字典

我有一个这样的字典:

/// <summary>
/// Gets the leave entitlement details.
/// </summary>
/// <value>The leave entitlement details.</value>
public Dictionary<string, EmployeeLeaveEntitlement> LeaveEntitlementDetails { get; set; }  
Run Code Online (Sandbox Code Playgroud)

我想将它映射到数据库.是否可以使用受保护或私有List <>?如:

/// <summary>
/// Gets the leave entitlement details.
/// </summary>
/// <value>The leave entitlement details.</value>
public Dictionary<string, EmployeeLeaveEntitlement> LeaveEntitlementDetails { get; set; } 

public List<EmployeeLeaveEntitlement> LeveEntitlementStore
{
    get
    {
        List<EmployeeLeaveEntitlement> leaveEntitlements = new List<EmployeeLeaveEntitlement>();

        foreach (KeyValuePair<string, EmployeeLeaveEntitlement> leaveType in LeaveEntitlementDetails)
        {
            leaveEntitlements.Add(leaveType.Value);
        }

        return leaveEntitlements;
    }
    set
    {
        foreach (EmployeeLeaveEntitlement item in value)
        { …
Run Code Online (Sandbox Code Playgroud)

c# dictionary ef-code-first

21
推荐指数
3
解决办法
4万
查看次数

EF Core:使用字典属性

有没有办法用 Entity Framework Core 填充字典属性?

出于性能原因,我们喜欢在应用程序而不是数据库中进行搜索。由于列表不能很好地扩展,我们喜欢使用字典。

例如(简化示例)

class Course
{
    public Dictionary<string, Person> Persons { get; set; }

    public int Id { get; set; }
}

class Person
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情

  • 天真地只是添加一个字典属性。这将导致以下错误:

    System.InvalidOperationException:无法映射属性“Persons”,因为它属于“Dictionary”类型,它不是受支持的原始类型或有效的实体类型。显式映射此属性,或使用 '[NotMapped]' 属性或使用 'OnModelCreating' 中的 'EntityTypeBuilder.Ignore' 忽略它。

  • 尝试添加价值转换(使用HasConversion),但转换仅适用于单个项目而不适用于集合。在HasMany已经提供了一个编译错误:

    builder
      .HasMany<Person>(c => c.Persons) //won't compile, Persons isn't a IEnumerable<Person>
      .WithOne().HasForeignKey("PersonId");
    
    Run Code Online (Sandbox Code Playgroud)
  • 创建一个自定义集合类(从继承Collection<T>和实施InsertItemSetItem等等) -不幸的是,这也不会工作,因为EF核心将项目添加到集合和第一后,将填补特性(至少我们OwnsOne性质,即不在演示案例中) - …

c# dictionary entity-framework-core entity-framework-core-3.1

10
推荐指数
2
解决办法
7788
查看次数

字典查找(O(1))vs Linq在哪里

什么是更快,我应该牺牲Linq标准来实现速度(假设字典查找真的更快)?那么让我详细说明一下:

我有以下内容:

List<Product> products = GetProductList();
Run Code Online (Sandbox Code Playgroud)

我需要根据某些属性搜索产品,例如序列号.我可以先创建一个字典,然后填充如下:

Dictionary<string, Product> dict = new Dictionary<string, Product>();
foreach(Product p in products)
{
    dict.Add(p.serial, p);
}
Run Code Online (Sandbox Code Playgroud)

在找到产品的时候,利用字典查找提供的O(1):

string some_serial = ...;
try { Product p = dict[some_serial]; } catch(KeyNotFoundException) { }
Run Code Online (Sandbox Code Playgroud)

或者,使用Linq:

Product p = products.Where(p => p.serial.Equals(some_serial)).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

Dict方法的缺点当然是这需要更多的内存空间,更多的代码来编写,更不优雅等等(尽管这大部分是有争议的).假设这是非因素.我应该采取第一种方法吗?

最后,我想确认上面的Linq方法的复杂性是否确实是O(n),我不知道它是如何更好的.

linq complexity-theory dictionary time-complexity

6
推荐指数
1
解决办法
2797
查看次数