使用实体框架查找
本文关键字:查找 框架 实体 | 更新日期: 2023-09-27 18:27:10
我正试图使用实体框架跨2个表进行查找,不幸的是我找不到方法,但我确实有sql查询,它可以满足我的要求
SQL查询
select top(1) p1.Percentage
from LookupTable p1 , [lookupTablePerUnit] p2
where p2.[LookupValue] <= @Value1 and p1.ID=p2.[LookupID]
order BY pr.range DESC
如果@Value1=6,则结果=52
以下是使用此查询的两个表(lookupTable&&LookTablePerUnit)的屏幕截图
(SELECT * FROM [DB].[dbo].[lookupTablePerUnit] p1 , [DB].[dbo].[LookupTable] p2
where p1.LookupTableID = p2.ID)
http://s15.postimage.org/m80zvn4mx/Captur2e.png
连接(您的查询使用SQL的隐式连接语法)在Linq中看起来与实体非常相似:
var query = from p1 in context.LookupTable
join p2 in context.lookupTablePerUnit on p1.ID equals p2.LookupID
where p2.LookupValue <= Value1
orderby p1.range descending
select p1.Percentage;
var result = query.FirstOrDefault();
必须对range
属性进行猜测,您的问题中有一个拼写错误,因此不清楚它是否可以归因于LookupTable
或lookupTablePerUnit
类似的东西(希望您使用的是C#):
int value1 = 6;
int percentage = (from p1 in context.LookupTable
from p2 in context.lookupTablePerUnit
where p2.LookupValue <= value1 and p1.ID=p2.LookupID
orderby p2.range descending
select p1.Percentage).First();
其中context
是您的ObjectContext
实例。请注意,实体框架可能会错误地将实体名称复数化,因此LookupTable
和LookupTablePerUnit
实际上可能类似于ObjectContext
中的LookupTables
和lookupTablePerUnits
(并且lookupTablePerUnit
可能大写)。