简体   繁体   English

使用实体框架查找

[英]Lookup using Entity Framework

I am trying to do lookup cross 2 tables using Entity Framework unfortunately I couldn't find way but I do have sql query which does what I want 我正在尝试使用Entity Framework跨2个表进行查找,很遗憾,我找不到方法,但是我确实有执行我想要的操作的SQL查询

SQL Query 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

if @Value1= 6 then The result = 52 如果@ Value1 = 6,则结果= 52

Here is a screenshot of two tables (lookupTable && LookTablePerUnit) using this query 这是使用此查询的两个表(lookupTable && LookTablePerUnit)的屏幕截图

(SELECT * FROM [DB].[dbo].[lookupTablePerUnit] p1 , [DB].[dbo].[LookupTable] p2
where p1.LookupTableID = p2.ID)

http://s15.postimage.org/m80zvn4mx/Captur2e.png http://s15.postimage.org/m80zvn4mx/Captur2e.png

A join (your query is using the implicit join syntax for SQL) would look very similar in Linq to Entities: 联接(您的查询正在使用SQL的隐式联接语法)在Linq中与Entities看起来非常相似:

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();

Had to take a guess on the range property you have a typo in your question so its not clear whether it can be attributed to LookupTable or lookupTablePerUnit 必须对range属性进行猜测,因此您的问题中有错别字,因此不清楚它是否可以归因于LookupTablelookupTablePerUnit

Something like this (hope you're using C#): 这样的事情(希望您使用的是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();

Where context is your ObjectContext instance. context是您的ObjectContext实例。 Note that Entity Framework may erroneously pluralized your entity names, so LookupTable and LookupTablePerUnit may actually be something like LookupTables and lookupTablePerUnits in your ObjectContext (and lookupTablePerUnit may be capitalized). 请注意,实体框架可能错误地使您的实体名称复数,因此LookupTableLookupTablePerUnit实际上可能类似于ObjectContext LookupTableslookupTablePerUnits (而lookupTablePerUnit可以大写)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM