LINQ JOIN查询中的EnumHelper.Parse

时间:2014-08-08 22:34:14

标签: c# linq

如果是重复的问题,我很抱歉,如果它请指出它。

我正在尝试在LINQ查询下执行并获得以下异常。任何人都可以告诉我如何解决这个问题

IEnumerable<ValueObjects.IR.IRTransactionGroupLink> codeSetsVOs = 
from itc in adjustmentContext.InformationRequestTransactionCodes
join itg in adjustmentContext.InformationRequestTransactionGroups
on itc.InformationRequestTransactionGroup equals itg.Group 
where itc.InformationRequestType == informationRequestType 
select new ValueObjects.IR.IRTransactionGroupLink 
{
IRTransactionGroupType = EnumHelper.Parse<IRTransactionGroupTypeEnum>(itg.Group.Trim(), true),
CountryCode = itc.CountryCode,
Description = itg.Description,
AppliesToAllCountries = itc.CountryCode.Equals("XX") ? true : false
};

例外是

LINQ to Entities无法识别方法'HL.Utility.Adjustment.InformationRequest.IRTransactionGroupTypeEnum Parse [IRTransactionGroupTypeEnum](System.String,Boolean)'方法,而且此方法无法转换为商店表达式

2 个答案:

答案 0 :(得分:0)

这是LINQ to Entitis的限制,它无法将任意C#代码转换为SQL。您可以通过在初始查询中保留IRTransactionGroupType为空(即将其转换为SQL并在数据库服务器中执行)来解决此问题,然后您可以在执行服务器查询后在客户端中设置属性的实际值,例如:

IEnumerable<ValueObjects.IR.IRTransactionGroupLink> query = 
                from itc in adjustmentContext.InformationRequestTransactionCodes
                join itg in adjustmentContext.InformationRequestTransactionGroups
                on itc.InformationRequestTransactionGroup equals itg.Group 
                where itc.InformationRequestType == informationRequestType 
                select new
                {
                    GroupType = itg.Group,
                    CountryCode = itc.CountryCode,
                    Description = itg.Description,
                    AppliesToAllCountries = itc.CountryCode.Equals("XX") ? true : false
                };
var serverQueryResult = query.ToList();
var clientQuery = from r in serverQueryResult
                  select new ValueObjects.IR.IRTransactionGroupLink 
                  {
                      IRTransactionGroupType = EnumHelper.Parse<IRTransactionGroupTypeEnum>(r.Group.Trim(), true),
                      CountryCode = r.CountryCode,
                      Description = r.Description,
                      AppliesToAllCountries = r.AppliesToAllCountries
                  };

答案 1 :(得分:0)

我将查询更改为

 IEnumerable<Entities.InformationRequestTransactionGroupLink> irTransactionGroupLinksEntities = 
(from itc in
this.adjustmentContext.InformationRequestTransactionCodes
join itg in this.adjustmentContext.InformationRequestTransactionGroups
on itc.InformationRequestTransactionGroup equals itg.Group
where itc.InformationRequestType == informationRequestType
select new
{
 sInformationRequestTransactionGroupType = itg.Group.Trim(),
 sCountryCode = itc.CountryCode,
 sDescription = itg.Description,
 sAppliesToAllCountries = itc.CountryCode.Trim().Equals("XX") ? true : false}).AsEnumerable()
.Select(irtgLink => new Entities.InformationRequestTransactionGroupLink
{
 InformationRequestTransactionGroupType = irtgLink.sInformationRequestTransactionGroupType,
 CountryCode = irtgLink.sCountryCode,
 Description = irtgLink.sDescription,
 AppliesToAllCountries = irtgLink.sCountryCode.Trim().Equals("XX") ? true : false
});