鉴于3个Bussines对象:
public class A
{
public long Id { get; set; }
}
public class B
{
public long ClassCId { get; set; }
public string ClassBFirstString { get; set; }
}
public class C
{
public long Id { get; set; }
public long ClassAId { get; set; }
}
和方法:
public IEnumerable<A> GetAllAbyClassBFirstStringFunction(string strg)
{
}
我想使用在对象C中具有ForeignKey(ClassAId
)的LINQ和类B的字符串(ClassBFirstString
)等于methodParameter({{1})来选择A的所有对象})。
我可以使用2个入口点
首先是一个strg
,所有的A对象都可以调用它_allObjectsOfA
第二个是IEQueryable<A>
,所有B的对象都可以调用它_allObjetsOfB
如果按照上述方法怎么做?
答案 0 :(得分:1)
鉴于这个模型:
List<A> ListOfA = new List<A>()
{
new A { Id = 1 }, new A { Id = 2 }, new A { Id = 3 }
};
List<B> ListOfB = new List<B>()
{
new B { ClassCId = 10, ClassBFirstString = "a"},
new B { ClassCId = 11, ClassBFirstString = "b"},
new B { ClassCId = 12, ClassBFirstString = "c"}
};
List<C> ListOfC = new List<C>()
{
new C { Id = 10, ClassAId = 1},
new C { Id = 11, ClassAId = 1},
new C { Id = 11, ClassAId = 3},
new C { Id = 12, ClassAId = 3}
};
方法IEnumerable<A> GetAllAbyClassBFirstStringFunction
的实现:
public static IEnumerable<A> GetAllAbyClassBFirstStringFunction(string strg)
{
var q = from a in ListOfA
join c in ListOfC on a.Id equals c.ClassAId
join b in ListOfB on c.Id equals b.ClassCId
where b.ClassBFirstString == strg
select a;
return q;
}
当这样调用时:
var q = GetAllAbyClassBFirstStringFunction("b");
返回以下结果:
带有[0] = {ConsoleApplication1.A}
的 Id = 1
带有[1] = {ConsoleApplication1.A}
Id = 3