如何在Linq to Entities中编写Like查询

时间:2016-04-28 06:10:10

标签: asp.net-mvc-5 linq-to-entities

我尝试了以下代码......

public ActionResult Search(string query)
    {
        using (DbAccess db=new DbAccess())
        {
            List<Student> studentsList = db.Students.Where(m=>m.name.Contains("d")==query).ToList();
            return View(studentsList);                
        }
    }

但如果我使用&#34;包含&#34;然后它给了我错误.. 请帮忙

2 个答案:

答案 0 :(得分:0)

我不知道&#34; d&#34;是的,但你应该这样做

@Override
public void validateVelocityRules(long organizationId, long accountId, long userId, Map<Integer, VelocityRuleChecker> rulesById, BigDecimal depositAmount, int depositItemCount) {
    List<VelocityScrutinyRuleRec> velocityScrutinyRuleTab = new ArrayList<>();
    for (Integer ruleId : rulesById.keySet()) {
        VelocityScrutinyRuleRec velocityScrutinyRuleRec = new VelocityScrutinyRuleRec();
        velocityScrutinyRuleRec.setId(ruleId);
        velocityScrutinyRuleRec.setIsFailed(null);
        velocityScrutinyRuleTab.add(velocityScrutinyRuleRec);
    }

    PLSQLrecord record = new PLSQLrecord();
    record.setTypeName("VELOCITY_SCRUTINY_RULES_REC");
    record.setCompatibleType("VELOCITY_SCRUTINY_RULES_REC");
    record.setJavaType(VelocityScrutinyRuleRec.class);
    record.addField("ID", JDBCTypes.NUMERIC_TYPE, 20,0);
    record.addField("IS_FAILED", JDBCTypes.CHAR_TYPE, 1);

    PLSQLCollection collection = new PLSQLCollection();
    collection.setTypeName("VELOCITY_SCRUTINY_RULES_TAB");
    collection.setCompatibleType("VELOCITY_SCRUTINY_RULES_TAB");
    collection.setJavaType(List.class);
    collection.setNestedType(record);

    PLSQLStoredProcedureCall call = new PLSQLStoredProcedureCall();
    call.setProcedureName("VELOCITY_PKG.SP_VALIDATE_RULES");
    call.addNamedArgument("p_org_id", JDBCTypes.INTEGER_TYPE);

或者,如果您希望名称包含查询和&#34; d&#34;然后

    DataReadQuery dataReadQuery = new DataReadQuery(call);

    dataReadQuery.addArgument("p_org_id");
    dataReadQuery.addArgument("p_rule", List.class);

    Vector args = new Vector();
    args.add(organizationId);
    args.add(velocityScrutinyRuleTab);

    ServerSession session =
            ((JpaEntityManager)entityManager.getDelegate()).getServerSession();

    ObjectRelationalDataTypeDescriptor descriptor = new ObjectRelationalDataTypeDescriptor();
    descriptor.setJavaClass(VelocityScrutinyRuleRec.class);
    descriptor.setTableName("VELOCITY_SCRUTINY_RULES_REC");
    descriptor.setStructureName("VELOCITY_SCRUTINY_RULES_REC");
    descriptor.setPrimaryKeyFieldName("ID");
    descriptor.addFieldOrdering("ID");
    descriptor.addFieldOrdering("IS_FAILED");

    descriptor.addDirectMapping("id", "ID");
    descriptor.addDirectMapping("isFailed", "IS_FAILED");
    session.addDescriptor(descriptor);
    Object object = JpaHelper.getEntityManager(entityManager).getActiveSession().executeQuery(dataReadQuery, args);

我希望这个肝脏

答案 1 :(得分:0)

在这种情况下,不需要使用“包含”:

public ActionResult Search(string query)
{
    using (DbAccess db=new DbAccess())
    {
        List<Student> studentsList = db.Students.Where(m=>m.name==query).ToList();
        return View(studentsList);                
    }
}

或者如果您想使用多个条件,则应使用“&amp;&amp;”条件之间的运算符,您可以使用此代码:

public ActionResult Search(string query)
{
    using (DbAccess db=new DbAccess())
    {
        List<Student> studentsList = db.Students
       .Where(m=>m.name.Contains("d") && m.name==query).ToList();
        return View(studentsList);                
    }
}