从另一个子查询中获取列值
Posted
技术标签:
【中文标题】从另一个子查询中获取列值【英文标题】:Getting Column Value from another Subquery 【发布时间】:2016-02-09 05:09:36 【问题描述】:我有一个 LINQ 查询。但我需要从另一个子查询中获取两列的值。这是我的 Linq 查询:
)from t in db.PUTAWAYs
join t0 in db.ASN_ITEM on t.AWB_NO equals t0.AWB_NO
join t1 in db.ASN_MASTER on t0.AWB_NO equals t1.AWB_NO
join t2 in db.ITEM_MASTER on t.ITEM_MASTER.ITEM_CODE equals t2.ITEM_CODE
join t3 in db.ASN_INPUT on t0.AWB_NO equals t3.AWB_NO
where
t3.ITEM == t2.ITEM_CODE &&
1 == 1 &&
(fromDate == "" || toDate == "" || (t0.REC_DATE.CompareTo(fromDate) >= 0 && t0.REC_DATE.CompareTo(toDate) <= 0)) &&
(AWB_NO == "" || (t0.AWB_NO == AWB_NO))
orderby
t.AWB_NO,
t0.REC_DATE,
t0.STYPE,
t2.PART_NO
select new ASNPutawayRep
AWB_NO = t.AWB_NO,
REC_DATE = t0.REC_DATE,
STYPE = t0.STYPE,
PART_NO = t2.PART_NO,
//LOCATION_AD = t.LOCATION_AD,
QNTY = t.QNTY,
//LOCATION_SD = t.LOCATION_SD,
REGION_ID = t.REGION_ID
).Distinct();
在上面查询的选择部分,我需要从SELECT LOC_NAME FROM LOCATION_MASTER WHERE LOC_CODE = t.LOCATION_AD
获取它,而不是直接取列t.LOCATION_AD
的值
我需要从SELECT LOC_NAME FROM LOCATION_MASTER where LOC_CODE = t.LOCATION_SD
获取价值,而不是t.LOCATION_SD
如何在 LINQ 中编写此代码。有没有办法做到这一点?
【问题讨论】:
为什么不能再添加一个join? 【参考方案1】:您可以使用let
子句。存储子表达式的结果以便在后续子句中使用它很有用。
示例:
(from t in db.PUTAWAYs
...
let locAd = from l in LOCATION_MASTER where LOC_CODE = t.LOCATION_SD select l.LOC_NAME
where
...
orderby
...
select new ASNPutawayRep
LOCATION_AD = locAd,
).Distinct();
另外,您可以不使用let
子句直接编写LINQ:
(from t in db.PUTAWAYs
...
where
...
orderby
...
select new ASNPutawayRep
LOCATION_AD = from l in LOCATION_MASTER where LOC_CODE = t.LOCATION_SD select l.LOC_NAME
).Distinct();
【讨论】:
我试过这个。但收到此错误:Cannot implicitly convert type 'System.Linq.IQueryable<string>' to 'string'
@LimnaD'silva:更新了,不过,这只是一个例子。您收到该错误是因为 LOC_NAME
的类型与 LOCATION_AD
不同。检查从数据库中取值的类型是否为string
,如果是,则需要检查ASNPutawayRep
类的LOCATION_AD
属性的类型。
两者都被声明为字符串。【参考方案2】:
你可以使用 AsQueryable 来实现这个
from t in db.PUTAWAYs
join t0 in db.ASN_ITEM on t.AWB_NO equals t0.AWB_NO
join t1 in db.ASN_MASTER on t0.AWB_NO equals t1.AWB_NO
join t2 in db.ITEM_MASTER on t.ITEM_MASTER.ITEM_CODE equals t2.ITEM_CODE
join t3 in db.ASN_INPUT on t0.AWB_NO equals t3.AWB_NO
where
t3.ITEM == t2.ITEM_CODE &&
1 == 1 &&
(fromDate == "" || toDate == "" || (t0.REC_DATE.CompareTo(fromDate) >= 0 && t0.REC_DATE.CompareTo(toDate) <= 0)) &&
(AWB_NO == "" || (t0.AWB_NO == AWB_NO))
orderby
t.AWB_NO,
t0.REC_DATE,
t0.STYPE,
t2.PART_NO
select new ASNPutawayRep
AWB_NO = t.AWB_NO,
REC_DATE = t0.REC_DATE,
STYPE = t0.STYPE,
PART_NO = t2.PART_NO,
LOCATION_AD = (from l in db.LOCATION_MASTER
where l.LOC_CODE = t.LOCATION_AD
select LocName)ToList().FirstorDefault(),
QNTY = t.QNTY,
LOCATION_SD = (from l in db.LOCATION_MASTER
where l.LOC_CODE = t.LOCATION_SD
select LocName).ToList().FirstorDefault(),
REGION_ID = t.REGION_ID
).Distinct();
【讨论】:
当我尝试这个时,我得到了错误'System.Linq.IQueryable<string>' does not contain a definition for FirstorDefault and no extension method 'FirstorDefault' accepting a first argument of type 'System.Linq.IQueryable<string>' could be found (are you missing a using directive or an assembly reference?)'
您在此代码所在的文件中有 Sytem.Linq using 语句吗??
奇怪...它应该可以工作....我认为当您使用 EF 时,整个查询作为表达式传递,当 QueryProvider 尝试执行表达式时它失败...我删除了 AsQueryable()从答案..请再试一次
您是否尝试过 SID 给出的另一个答案?
是的..我也试过了。但同样的错误存在。以上是关于从另一个子查询中获取列值的主要内容,如果未能解决你的问题,请参考以下文章