gpt4 book ai didi

c# - 如何使用 Join 和 Where 返回 IList?

转载 作者:太空狗 更新时间:2023-10-29 20:33:33 26 4
gpt4 key购买 nike

如何使用 Linq 或 EF Join 等工具在 C# MVC 中实现连接和位置?

这是我要实现的等效 SQL。

select * from promotion P
JOIN PromotionsClaimed PC
on PC.PromotionId = P.objectid
where PC.userId = @USERID

此方法应返回用户的促销列表。首先,我得到所有促销的列表,然后我得到用户声明的促销的综合列表。这是我目前所拥有的。

    public IList<Promotion> GetRewardsForUser(string userId)
{
//a list of all available promotions
IList<Promotion> promos = _promotionLogic.Retrieve();

//contains a list of Promotion.objectIds for that user
IList<PromotionsClaimed> promosClaimed = _promotionsClaimedLogic.RetrieveByCriteria(t => t.userId == userId);

//should return a list of the Promotion name and code for the rewards claimed by user, but a complete list of Promotion entities would be fine
var selectedPromos =
from promo in promos
join promoClaimed in promosClaimed on promo.objectId equals promoClaimed.PromotionId
select new { PromoName = promo.Name, PromoCode = promo.Code };

return selectedPromos;
}

我意识到这里有很多问题。我正在尝试学习 Linq 和 Entity Framework ,但我不知道如何将 where 子句添加到 IList 或者是否有更简单的方法来完成此操作。

在我看来,似乎有一种方法可以过滤促销列表,其中包含 promosClaimed 列表中的 Promotion.objectId,但我不知道语法。

最佳答案

 public IList<Promotion> GetRewardsForUser(string userId)
{
//a list of all available promotions
IList<Promotion> promos = _promotionLogic.Retrieve();

//contains a list of Promotion.objectIds for that user
IList<PromotionsClaimed> promosClaimed = _promotionsClaimedLogic.RetrieveByCriteria(t => t.userId == userId);

//should return a list of the Promotion name and code for the rewards claimed by user, but a complete list of Promotion entities would be fine
var selectedPromos =
(from promo in promos
join promoClaimed in promosClaimed on promo.objectId equals promoClaimed.PromotionId
select new { PromoName = promo.Name, PromoCode = promo.Code }).ToList();

return selectedPromos;
}

关于c# - 如何使用 Join 和 Where 返回 IList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30967607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com