主キーに基づいてエンティティの操作を実行する一般的なlinqtoSQLを作成しようとしています。私が働いているいくつかのエンティティは、複合主キーを持っています。エンティティが主キーを持っている唯一の要件でlinqtoSQLの主キーの使用
if(PrimaryKey(foo) == PrimaryKey(bar)) ...
または、
from oldEntity in oldTableOrCollection
join newEntity in newTableOrCollection
on PrimaryKeyOf(oldEntity) equals PrimaryKeyOf(newEntity)
select new {oldEntity, newEntity}
:
基本的に私は、次のようなことを行うことができるようにしたいです。ここで
は、私がこれまでに見つけたものです:
それはlinqtosqlは(プロパティ名が一致して)比較のために匿名型を使用します。ここで、このような
var query = from row in oneTableOfRows
join otherRow in anotherTableOfRows on
new { row.Column1, row.Column2 }
equals
new { otherRow.Column1, otherRow.Column2}
select ...;
として物事を行うことが可能です。
私はできジェネリック行タイプを許可するために参加するように列を選択するため、次に抽象メソッド:keySelectorは主キーを選択する場所
void DoQuery<RowType,KeyType>(Table<RowType> oneTableOfRows,
Table<RowType> anotherTableOfRows,
Func<RowType, KeyType> keySelector)
{
var query = from row in oneTableOfRow
join otherRow in anotherTableOfRows on
keySelector(row)
equals
keySelector(otherRow)
select ...;
}
...
Func<rowType, keyType> myKeySelector = row => new { row.Column1, row.Column2 };
DoQuery(table, otherTable, myKeySelector);
は私が今まで移動しようとしている、さ任意のRowTypeの私は自分のエンティティを生成するのにhttp://www.codeplex.com/l2st4に基づくカスタムテンプレートを使用しています(それ自体はVSのデフォルトとかなり似ています)。 私は理想的には、生成された各RowTypeに、dbmlから集められた主キーを選択する機能を与えることを望んでいます。これまでouptutは、次のようになります。
public interface IPrimaryKeyEntity<PrimaryKeyType>
{
PrimaryKeyType PrimaryKey { get; }
}
//Here, Product is a table with composite primary key based on its ProductID and it's ProductPriceVersionID columns
//I've tried using both class and struct as the primary key type for entities
public class ProductPrimaryKey
{
public Guid ProductID;
public Guid ProductPriceVersionID;
}
public partial class Product : IPrimaryKeyEntity<ProductPrimaryKey>,
INotifyPropertyChanging, INotifyPropertyChanged
{
#region IPrimaryKeyEntity Implementation
public ProductPrimaryKey PrimaryKey
{
get
{ return new ProductPrimaryKey()
{
ProductID = this.ProductID,
ProductPriceVersionID = this.ProductPriceVersionID
};
}
}
#endregion
...
//Rest is mostly standard LinqToSql entity generation
}
元狙いに行く、私は今、すべての私の主キーエンティティの次をコンパイルすることができます
from oldEntity in oldTableOrCollection
join newEntity in newTableOrCollection
on oldEntity.PrimaryKey equals newEntity.PrimaryKey
select new {oldEntity, newEntity}
ただし、実行時に、私が手悪名高い[PrimaryKey] "SQLへのサポートされた変換がありません"例外。
誰でも向上させることができた場合、私は...私がLinq.Expressions
にかなり慣れていないですし、私の状況にそれを適用しようとまだ進展がなかったてきたがExpression
年代を使用する必要があるSQLに変換するために
をそれを理解....私は知って感謝されると思い、これまでに得たか、一般的にはより良い方法を持ってきたもので
乾杯
デバッガで(動作中の)クエリ用に生成された 'Expression'を探索するのが良いでしょう。これは「非公開メンバー」>「queryExpression」の下にあります。私もこれに新しいですが、私は何かを見つける場合あなたに教えてあげます:) –
@ VladislavZorov:提案をありがとう!私は明日オフィスに戻ったときにそれが助けになるかどうか確かに分かるでしょう。 – EdF