小编yul*_*lun的帖子

GetHashCode()用于长原语

我正在为LINQ distinct表达式编写EqualityComparer,我对GetHashCode重载方法不太确定.以下代码是否正确?Id属性是一个很长的原语.

public int GetHashCode(Deal obj)
{
   return ((int)obj.Id) ^ ((int)(obj.Id >> 32)); ;
}
Run Code Online (Sandbox Code Playgroud)

.net c# iequalitycomparer

5
推荐指数
1
解决办法
5545
查看次数

Laravel Custom Pivot Table关系和渴望加载?

我在为我的一个项目创建模式/模型时遇到问题,并希望在这里得到一些帮助.

我目前有3张桌子:配件,产品和数据透视表product_accessory

<?php 

Schema::create('accessories', function(Blueprint $table)
{
    $table->increments('id');
}

Schema::create('products', function(Blueprint $table) 
{
    $table->increments('id');
}

Schema::create('product_accessory', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('product_id')->unsigned();
    $table->integer('accessory_id')->unsigned();
    $table->foreign('product_id')->references('id')->on('products');
    $table->foreign('accessory_id')->references('id')->on('accessories');
}
Run Code Online (Sandbox Code Playgroud)

现在问题是我需要添加另一种产品类型'适配器',它最终将取决于数据透视表关系,即适配器需要与产品和附件相关...

更新 以下是我当前的product_accessory_adaptor表的方式

Schema::create('product_accessory_adaptor', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('product_accessory_id')->unsigned();
    $table->foreign('product_accessory_id')->references('id')->on('product_accessory');
}
Run Code Online (Sandbox Code Playgroud)

这样,我就可以拥有许多与产品和配件相关的适配器.我的问题是如何在雄辩中模拟这种关系?

这就是我现在拥有的:

自定义枢轴模型:

class ProductAccessory extends Pivot {
   protected $table = 'product_accessory';

   public function product()
   {
      return $this->belongsTo('Product');
   }

   public function accessory()
   {
     return $this->belongsTo('Accessory');
   }

   public function adaptors() {
     return $this->hasMany('Adaptor', 'product_accessory_id'); 
   } 
}
Run Code Online (Sandbox Code Playgroud)

产品和附件模型

class Accessory extends Eloquent {

   public function products() …
Run Code Online (Sandbox Code Playgroud)

database laravel eloquent

5
推荐指数
1
解决办法
4342
查看次数

使用LINQ和Entity框架检查c#var中的空值

我对LINQ和Entity框架以及c#中的var关键字都很陌生,所以如果这听起来像是一个"新手"问题,请原谅我.

在执行以下操作后,我在检查空值时遇到问题:

var entry = myDB.Entries.Where(e => e.Email == entry.Email);
Run Code Online (Sandbox Code Playgroud)

即使数据库中不存在电子邮件,条目也不等于null.

因此,在执行下一批语句之前,if (entry == null)我不得不if (entry.Count() < 1)检查现有的条目.是否有任何理由不将变量视为null?

c# linq lambda entity-framework var

2
推荐指数
1
解决办法
1万
查看次数