根据输入枚举参数选择列

时间:2017-04-03 09:24:03

标签: c# linq select design-patterns enums

我有表格列

{provider}Enabled

在代码中,我想根据输入枚举选择true/false值 - enum Provider { Google, Facebook, Twitter } private async Task CheckSettingsAsync(ISeedSession<IDbSession> seedSession, IUserInfo user, Provider provider){ ... var userSettingsTask = seedSession .Query<UserSettings>() .Where(us => us.UserId == user.UserId) .Select(us => us.GoogleEnabled) // if provider value is "Google" .SingleAsync(); ... } 的值

switch

有没有比创建一些 <?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace="http://www.abc.com.tr/om/wsdl/EBS_PRODUCT_OFFER_OPERATIONS" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="http://www.abc.com.tr/om/wsdl/EBS_PRODUCT_OFFER_OPERATIONS" xmlns:ns="http://www.abc.com.tr/om/EBS_PRODUCT_OFFER_OPERATIONS" xmlns:fault="http://www.abc.com.tr/Fault"> <wsdl:documentation>This service provides operations to retrieve prepaid or postpaid product details.</wsdl:documentation> <wsdl:types> <xs:schema targetNamespace="http://www.abc.com.tr/om/EbsProductOfferOperations" elementFormDefault="qualified"> <xs:import namespace="http://www.abc.com.tr/om/EBS_PRODUCT_OFFER_OPERATIONS" schemaLocation="../aveaXsd/EBS_PRODUCT_OFFER_OPERATIONS.xsd"/> <xs:import namespace="http://www.abc.com.tr/Fault" schemaLocation="../aveaXsd/Fault.xsd"/> </xs:schema> </wsdl:types> <wsdl:message name="RetrievePostpaidCommitmentOfferRequest"> <wsdl:part name="parameter" element="ns:RetrievePostpaidCommitmentOfferReq"/> </wsdl:message> <wsdl:message name="RetrievePostpaidCommitmentOfferResponse"> <wsdl:part name="parameter" element="ns:RetrievePostpaidCommitmentOfferResp"/> </wsdl:message> <wsdl:message name="RetrievePostpaidFreeUnitOfferRequest"> <wsdl:part name="parameter" element="ns:RetrievePostpaidFreeUnitOfferReq"/> </wsdl:message> <wsdl:message name="RetrievePostpaidFreeUnitOfferResponse"> <wsdl:part name="parameter" element="ns:RetrievePostpaidFreeUnitOfferResp"/> </wsdl:message> <wsdl:message name="RetrievePostpaidServiceOfferRequest"> <wsdl:part name="parameter" element="ns:RetrievePostpaidServiceOfferReq"/> </wsdl:message> <wsdl:message name="RetrievePostpaidServiceOfferResponse"> <wsdl:part name="parameter" element="ns:RetrievePostpaidServiceOfferResp"/> </wsdl:message> <wsdl:message name="RetrievePostpaidTariffOfferRequest"> <wsdl:part name="parameter" element="ns:RetrievePostpaidTariffOfferReq"/> </wsdl:message> <wsdl:message name="RetrievePostpaidTariffOfferResponse"> <wsdl:part name="parameter" element="ns:RetrievePostpaidTariffOfferResp"/> </wsdl:message> <wsdl:message name="RetrievePrepaidBonusOfferRequest"> <wsdl:part name="parameter" element="ns:RetrievePrepaidBonusOfferReq"/> </wsdl:message> <wsdl:message name="RetrievePrepaidBonusOfferResponse"> <wsdl:part name="parameter" element="ns:RetrievePrepaidBonusOfferResp"/> </wsdl:message> <wsdl:message name="RetrievePrepaidServiceOfferRequest"> <wsdl:part name="parameter" element="ns:RetrievePrepaidServiceOfferReq"/> </wsdl:message> <wsdl:message name="RetrievePrepaidServiceOfferResponse"> <wsdl:part name="parameter" element="ns:RetrievePrepaidServiceOfferResp"/> </wsdl:message> <wsdl:message name="RetrievePrepaidTariffOfferRequest"> <wsdl:part name="parameter" element="ns:RetrievePrepaidTariffOfferReq"/> </wsdl:message> <wsdl:message name="RetrievePrepaidTariffOfferResponse"> <wsdl:part name="parameter" element="ns:RetrievePrepaidTariffOfferResp"/> </wsdl:message> <wsdl:message name="RetrievePromotionOfferRequest"> <wsdl:part name="parameter" element="ns:RetrievePromotionOfferReq"/> </wsdl:message> <wsdl:message name="RetrievePromotionOfferResponse"> <wsdl:part name="parameter" element="ns:RetrievePromotionOfferResp"/> </wsdl:message> <wsdl:message name="CustomerProductAssignmentRequest"> <wsdl:part name="parameter" element="ns:CustomerProductAssignmentRequest"/> </wsdl:message> <wsdl:message name="CustomerProductAssignmentResponse"> <wsdl:part name="parameter" element="ns:CustomerProductAssignmentResponse"/> </wsdl:message> <wsdl:message name="FaultMessage"> <wsdl:part name="fault" element="fault:ExceptionSchema"/> </wsdl:message> <wsdl:portType name="EbsProductOfferOperationsPortType"> <wsdl:operation name="retrievePostpaidCommitmentOffer"> <wsdl:documentation>This method can be used to return details for postpaid commitment (Taahhüt)</wsdl:documentation> <wsdl:input message="tns:RetrievePostpaidCommitmentOfferRequest"/> <wsdl:output message="tns:RetrievePostpaidCommitmentOfferResponse"/> <wsdl:fault name="fault" message="tns:FaultMessage"/> </wsdl:operation> <wsdl:operation name="retrievePostpaidFreeUnitOffer"> <wsdl:documentation>This method can be used to return details for postpaid freeunit</wsdl:documentation> <wsdl:input message="tns:RetrievePostpaidFreeUnitOfferRequest"/> <wsdl:output message="tns:RetrievePostpaidFreeUnitOfferResponse"/> <wsdl:fault name="fault" message="tns:FaultMessage"/> </wsdl:operation> <wsdl:operation name="retrievePostpaidServiceOffer"> <wsdl:documentation>This method can be used to return details for a postpaid service.</wsdl:documentation> <wsdl:input message="tns:RetrievePostpaidServiceOfferRequest"/> <wsdl:output message="tns:RetrievePostpaidServiceOfferResponse"/> <wsdl:fault name="fault" message="tns:FaultMessage"/> </wsdl:operation> <wsdl:operation name="retrievePostpaidTariffOffer"> <wsdl:documentation>This method can be used to return details for a postpaid tariff.</wsdl:documentation> <wsdl:input message="tns:RetrievePostpaidTariffOfferRequest"/> <wsdl:output message="tns:RetrievePostpaidTariffOfferResponse"/> <wsdl:fault name="fault" message="tns:FaultMessage"/> </wsdl:operation> <wsdl:operation name="retrievePrepaidBonusOffer"> <wsdl:documentation>This method can be used to return details for a prepaid bonus.</wsdl:documentation> <wsdl:input message="tns:RetrievePrepaidBonusOfferRequest"/> <wsdl:output message="tns:RetrievePrepaidBonusOfferResponse"/> <wsdl:fault name="fault" message="tns:FaultMessage"/> </wsdl:operation> <wsdl:operation name="retrievePrepaidServiceOffer"> <wsdl:documentation>This method can be used to return details for a prepaid service product.</wsdl:documentation> <wsdl:input message="tns:RetrievePrepaidServiceOfferRequest"/> <wsdl:output message="tns:RetrievePrepaidServiceOfferResponse"/> <wsdl:fault name="fault" message="tns:FaultMessage"/> </wsdl:operation> <wsdl:operation name="retrievePrepaidTariffOffer"> <wsdl:documentation>This method can be used to return details for a prepaid tariff product.</wsdl:documentation> <wsdl:input message="tns:RetrievePrepaidTariffOfferRequest"/> <wsdl:output message="tns:RetrievePrepaidTariffOfferResponse"/> <wsdl:fault name="fault" message="tns:FaultMessage"/> </wsdl:operation> <wsdl:operation name="retrievePromotionOffer"> <wsdl:input message="tns:RetrievePromotionOfferRequest"/> <wsdl:output message="tns:RetrievePromotionOfferResponse"/> <wsdl:fault name="fault" message="tns:FaultMessage"/> </wsdl:operation> <wsdl:operation name="customerProductAssignment"> <wsdl:input message="tns:CustomerProductAssignmentRequest"/> <wsdl:output message="tns:CustomerProductAssignmentResponse"/> <wsdl:fault name="fault" message="tns:FaultMessage"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="EbsProductOfferOperationsBinding" type="tns:EbsProductOfferOperationsPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="retrievePostpaidCommitmentOffer"> <soap:operation soapAction="retrievePostpaidCommitmentOffer"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> <wsdl:fault name="fault"> <soap:fault name="fault" use="literal"/> </wsdl:fault> </wsdl:operation> <wsdl:operation name="retrievePostpaidFreeUnitOffer"> <soap:operation soapAction="retrievePostpaidFreeUnitOffer"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> <wsdl:fault name="fault"> <soap:fault name="fault" use="literal"/> </wsdl:fault> </wsdl:operation> <wsdl:operation name="retrievePostpaidServiceOffer"> <soap:operation soapAction="retrievePostpaidServiceOffer"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> <wsdl:fault name="fault"> <soap:fault name="fault" use="literal"/> </wsdl:fault> </wsdl:operation> <wsdl:operation name="retrievePostpaidTariffOffer"> <soap:operation soapAction="retrievePostpaidTariffOffer"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> <wsdl:fault name="fault"> <soap:fault name="fault" use="literal"/> </wsdl:fault> </wsdl:operation> <wsdl:operation name="retrievePrepaidBonusOffer"> <soap:operation soapAction="retrievePrepaidBonusOffer"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> <wsdl:fault name="fault"> <soap:fault name="fault" use="literal"/> </wsdl:fault> </wsdl:operation> <wsdl:operation name="retrievePrepaidServiceOffer"> <soap:operation soapAction="retrievePrepaidServiceOffer"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> <wsdl:fault name="fault"> <soap:fault name="fault" use="literal"/> </wsdl:fault> </wsdl:operation> <wsdl:operation name="retrievePrepaidTariffOffer"> <soap:operation soapAction="retrievePrepaidTariffOffer"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> <wsdl:fault name="fault"> <soap:fault name="fault" use="literal"/> </wsdl:fault> </wsdl:operation> <wsdl:operation name="retrievePromotionOffer"> <soap:operation soapAction="retrievePromotionOffer"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> <wsdl:fault name="fault"> <soap:fault name="fault" use="literal"/> </wsdl:fault> </wsdl:operation> <wsdl:operation name="customerProductAssignment"> <soap:operation soapAction="customerProductAssignment"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> <wsdl:fault name="fault"> <soap:fault name="fault" use="literal"/> </wsdl:fault> </wsdl:operation> </wsdl:binding> <wsdl:service name="EbsProductOfferOperations"> <wsdl:port name="EbsProductOfferOperationsPortType" binding="tns:EbsProductOfferOperationsBinding"> <soap:address location="http://localhost:1233/MTSEsb-AveaEsbModel-context-root/EbsProductOfferOperationsPortTypeImplService/EbsProductOfferOperations/EbsProductOfferOperations?wsdl"/> </wsdl:port> </wsdl:service> </wsdl:definitions> 更好的模式/解决方案来选择我的价值?

1 个答案:

答案 0 :(得分:1)

您可以手动构建表达式us => us.GoogleEnabled,如下所示:

var argument = Expression.Variable(typeof(UserSettings));
// assuming `provider` is enum with values "Google" etc
var accessor = (Expression<Func<UserSettings, bool>>) Expression.Lambda(
    Expression.PropertyOrField(argument, provider.ToString() + "Enabled"), argument);

然后像这样使用它:

var userSettingsTask = seedSession
   .Query<UserSettings>()
   .Where(us => us.UserId == user.UserId)
   .Select(accessor) // < here
   .SingleAsync();