我有許多從相同基類繼承的LINQ類。每個Linq類都有它自己的表,然後基類是到另一個表的一種鏈接。我會試着說明的關係:具有接口的C#Linq查詢
//Base Class
abstract class Element<T>
{
public string Name {get; set;}
public string Species {get; set;}
}
public partial class DogElement : Element<DogElement>
{
public int Legs {get; set;}
public bool hasHair {get; set;}
}
public partial class CatElement : Element<CatElement>
{
public bool hasWiskers {get; set}
}
所以DogElement和CatElement是數據庫中不同的物理表。 Species類將從Species表中讀取。我基本上想在Element中有一個通用函數,它將T表與Species表分組,並在設置.Species之後將結果作爲T返回。
//Assume T is DogElement for this example
public IQueryable<T> Collection
{
using (DataBase db = new DataBase())
{
var k = from t in db.GetTable<T>()
where t.SomeID == SomeOtherID
select t;
k = { {Legs = 4, HasHair = True, Species = <null>, Name = <null>}, ...}
I basically want a query that will return an IQueryable<DogElement> with Name
and Species set from a join query. What is the best way to set the Name and
Species values?
return k;
}
類抽象元素→抽象類元素 –
shenhengbin