c# – 如何使用Join和Where返回IList?
发布时间:2020-12-15 07:46:03 所属栏目:百科 来源:网络整理
导读:如何使用 Linq或EF Join之类的东西在C#MVC中实现连接和位置? 这是我试图实现的等效SQL. select * from promotion PJOIN PromotionsClaimed PCon PC.PromotionId = P.objectidwhere PC.userId = @USERID 此方法应返回用户的促销列表.首先,我获得所有促销的列
如何使用
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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |