Use*_*008 7 c# linq entity-framework
如何使用Linq或EF Join之类的东西在C#MVC中实现连接和位置?
这是我试图实现的等效SQL.
select * from promotion P
JOIN PromotionsClaimed PC
on PC.PromotionId = P.objectid
where PC.userId = @USERID
Run Code Online (Sandbox Code Playgroud)
此方法应返回用户的促销列表.首先,我获得所有促销的列表,然后我获得用户声明的促销的综合列表.这就是我到目前为止所拥有的.
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;
}
Run Code Online (Sandbox Code Playgroud)
我意识到这里有很多问题.我正在尝试学习Linq和Entity Framework,但我不知道如何将where子句添加到IList,或者是否有更简单的方法来实现它.
在我看来,像有将过滤它包含推广名单的方式Promotion.objectId在promosClaimed列表中,但我不知道语法.
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
733 次 |
| 最近记录: |