Улучшение LANDDA LINQ

Из docs :

list.insert (i, x) Вставить элемент в заданную позицию. Первый аргумент - это индекс элемента, перед которым нужно вставить, поэтому a.insert (0, x) вставлен в начале списка, а a.insert (len (a), x) эквивалентен a.append ( x).

blockquote>

Итак, технически, когда вы делаете a.insert(100, 100), это гарантирует, что 100 будет вставлен в индекс до 100, который, оказывается, в этом случае индекс 3.

Далее, мы можем взглянуть на реализацию :

static int
ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
{
    Py_ssize_t i, n = Py_SIZE(self);
    PyObject **items;
    if (v == NULL) {
        PyErr_BadInternalCall();
        return -1;
    }
    if (n == PY_SSIZE_T_MAX) {
        PyErr_SetString(PyExc_OverflowError,
            "cannot add more objects to list");
        return -1;
    }

    if (list_resize(self, n+1) == -1)
        return -1;

    if (where < 0) {
        where += n;
        if (where < 0)
            where = 0;
    }
    if (where > n)  // <-- Here the implementation handles indexes > list-len
        where = n;
    items = self->ob_item;
    for (i = n; --i >= where; )
        items[i+1] = items[i];
    Py_INCREF(v);
    items[where] = v;
    return 0;
}

-1
задан Drag and Drop 13 July 2018 в 13:54
поделиться

3 ответа

Я смог понять это с помощью некоторых ответов, и я сам тестировал его. Вот мое решение:

incidentVm.LstSpecialCategories = db.TBL_AssocIncidentSpecialCat
                                    .Where(t => t.IncidentId == incidentVm.ID)
                                    .Join(db.TBL_SpecialCategories,
                                        ik => ik.SpecialCategoriesId,
                                        ok => ok.Id,
                                        (ik, ok) => ok.SpecialCategory
                                    )
                                    .ToList();

Спасибо за вашу помощь.

1
ответ дан Drag and Drop 17 August 2018 в 12:51
поделиться

Вы можете попробовать LINQ query-style join:

incidentVm.LstSpecialCategories = (from aispc in db.TBL_AssocIncidentSpecialCat
                                    join spc in db.TBL_SpecialCategories
                                    on aispc.SpecialCategoriesId equals lspc.Id
                                    where aispc.IncidentId == incidentVm.ID
                                    select lspc.SpecialCategory).ToList();
0
ответ дан LolPython 17 August 2018 в 12:51
поделиться
1
ответ дан Drag and Drop 6 September 2018 в 09:16
поделиться
Другие вопросы по тегам:

Похожие вопросы: