使用Linq to SQL从数量不在fromquantity和ToQuantity之间的表中获取记录
本文关键字:之间 ToQuantity 记录 获取 fromquantity SQL to Linq 使用 | 更新日期: 2023-09-27 18:02:47
我有两个表,以下tb_Types
和tb_TypePrices
:
tb_Type
-------
id
title
tb_TypePrices
-------------
id
typeId
fromQuantity
ToQuantity
Supplier
Price
这些表有父子关系
现在我想从tb_Types
和tb_TypePrices
中获取数据,其中typeId=mytypeId
和parameterQuantity
不在fromQuantity
和ToQuantity
之间。
from t in tb_Type
join tp in tb_TypePrices on t.id equals tp.typeId
where t.id == mytypeId &&
(parameterQuantity < tp.fromQuantity ||
tp.ToQuantity < parameterQuantity)
select new { Type = t, TypePrice = tp }
或者如果你想在一个对象中过滤所有类型的价格:
from t in tb_Type
join tp in tb_TypePrices on t.id equals tp.typeId into g
where t.id == mytypeId
select new {
Type = t,
Prices = g.Where(x => parameterQuantity < x.fromQuantity ||
x.ToQuantity < parameterQuantity)
}
try this:
var data = from t1 in tb_Type
join t2 in tb_TypePrices
on t1.id equals t2.typeId
where t2.fromQuantity < parameterQuantity || t2.ToQuantity > parameterQuantity
select new { Type = t1, TypePrices = t2 };
假设您有外键关系,那么您应该能够使用
var query = tb_TypePrices
.Where( t => t.fromQuantity < parameterQuantity ||
t.ToQuantity > parameterQuantity)
.Select( t => new
{
t.fromQuantity,
t.ToQuantity,
... etc,
Title=t.tb_Type.title
};