按对象集的值过滤对象列表

时间:2018-05-14 21:33:58

标签: java lambda

我需要检索具有特定角色的用户列表。用户对象包含一组角色,其id和角色名称为String。 与用户一起使用JSON示例:

#include <stdio.h>

int main(void) 
{
    int charsToRead = 6;
    char inputString[9];
    char* inputPointer = inputString + 1;
    inputString[0] = '"';
    inputString[7] = '"';
    inputString[8] = '\0';
    printf("type something with quotes\n");
    scanf("\"%6c\"", inputPointer);
    printf("the captured string is %s", inputString);
    return 0;
}

我可以通过在for循环中使用if循环来实现这一点。用户只有一个角色(由于某些原因,我必须使用set):

def stop_motors():
    p1 = Process(target=stop_left());
    p2 = Process(target=stop_right());
    p1.start();
    p2.start();

def stop_left():
    MotorLeft.stop(stop_action='hold');
    MotorLeft.reset();

def stop_right():
    MotorRight.stop(stop_action='hold');
    MotorRight.reset();

还有其他更简单的方法,比如lambda来过滤列表中的内容吗?我试过这个解决方案Java 8 lambda list inside list filter 它过滤但它也复制了对象。

1 个答案:

答案 0 :(得分:1)

可以使用流API进行如下操作:

    [HttpPost]
    [AuthorizeExtended(Roles = "User, Admin")]
    [Route("api/BillingToDo/GenerateInvoices")]
    public async Task<IHttpActionResult> GenerateInvoices(BillingToDoGenerateInvoice model)
    {
        try
        {
            using (var db = new YOUREntities())
            {
                //Build your record
                var tableSchema = new List<SqlMetaData>(1)
                {
                    new SqlMetaData("Id", SqlDbType.UniqueIdentifier)
                }.ToArray();

                //And a table as a list of those records
                var table = new List<SqlDataRecord>();

                for (int i = 0; i < model.elements.Count; i++)
                {
                    var tableRow = new SqlDataRecord(tableSchema);
                    tableRow.SetGuid(0, model.elements[i]);
                    table.Add(tableRow);
                }

                //Parameters for your query
                SqlParameter[] parameters =
                {
                    new SqlParameter
                    {
                        SqlDbType = SqlDbType.Structured,
                        Direction = ParameterDirection.Input,
                        ParameterName = "listIds",
                        TypeName = "[dbo].[GuidList]", //Don't forget this one!
                        Value = table
                    },
                    new SqlParameter
                    {
                        SqlDbType = SqlDbType.UniqueIdentifier,
                        Direction = ParameterDirection.Input,
                        ParameterName = "createdBy",
                        Value = CurrentUser.Id
                    },
                    new SqlParameter
                    {
                        SqlDbType = SqlDbType.Int,
                        Direction = ParameterDirection.Output, // output!
                        ParameterName = "success"
                    },
                    new SqlParameter
                    {
                        SqlDbType = SqlDbType.NVarChar,
                        Size = -1,                             // "-1" equals "max"
                        Direction = ParameterDirection.Output, // output too!
                        ParameterName = "errorMessage"
                    }
                };

                //Do not forget to use "DoNotEnsureTransaction" because if you don't EF will start it's own transaction for your SP.
                //In that case you don't need internal transaction in DB or you must detect it with @@trancount and/or XACT_STATE() and change your logic
                await db.Database.ExecuteSqlCommandAsync(TransactionalBehavior.DoNotEnsureTransaction,
                    "exec GenerateInvoice @listIds, @createdBy, @success out, @errorMessage out", parameters);

                //reading output values:
                int retValue;
                if (parameters[2].Value != null && Int32.TryParse(parameters[2].Value.ToString(), out retValue))
                {
                    if (retValue == 1)
                    {
                        return Ok("Invoice generated successfully");
                    }
                }

                string retErrorMessage = parameters[3].Value?.ToString();

                return BadRequest(String.IsNullOrEmpty(retErrorMessage) ? "Invoice was not generated" : retErrorMessage);
            }
        }
        catch (Exception e)
        {
            return BadRequest(e.Message);
        }
    }
}