如何使用lambda查询找到max-ID?

时间:2014-12-21 20:11:12

标签: c# linq

我想通过选择ID_Artiest找到下一个ID(ID_Titel)。我通过使用LINQ(lambda-query)来尝试它。我做错了什么。

以示例:

enter image description here

对于ID_Artiest 2,我想要ID_Titel n + 1。

首先我尝试没有.DefaultIfEmpty()。然后我收到一条消息:序列包含无元素

其次我尝试使用.DefaultIfEmpty():然后我收到消息:对象引用未设置为对象的实例。

这是来源:

private void UpdateTitel(NoteringDataType itemDezeWeek)
...
...
    TitelDataType titel = new TitelDataType();
    if (Titelslijst.Count > 0)
        titel.ID_Titel = Titelslijst.Where(t => t.ID_Artiest == itemDezeWeek.ID_Artiest).OrderBy(t => t.ID_Titel).DefaultIfEmpty().Max(t => t.ID_Titel) + 1;
    else
        titel.ID_Titel = 1;

3 个答案:

答案 0 :(得分:3)

如果第一个Where找不到任何匹配的值,则会出现两个错误。

请试试这个:

titel.ID_Titel = Titelslijst.Where(t => t.ID_Artiest == itemDezeWeek.ID_Artiest)
                            .Select(t => t.ID_Titel)
                            .OrderByDescending(t => t)
                            .FirstOrDefault() + 1;

由于Select将集合转换为int的枚举(我假设),如果.FirstOrDefault()没有0,则Where()应返回.DefaultIfEmpty() #39; t返回任何匹配。在您的代码中,IEnumerable<T>会返回包含null的{​​{1}},因为它是在Titelslijst包含的任何枚举中调用的。

答案 1 :(得分:2)

显然,当列表中的任何内容与您的where条件匹配时,您会看到错误。在这种情况下,DefaultIfEmpty只会为您提供一个包含null的列表,这对您来说无济于事。

为了避免这种情况,如果没有找到条目,您应该将ID分配给1。所以逻辑应该是:

TitelDataType titel = new TitelDataType();
int id = 1;
if (Titelslijst.Count > 0)
{
    var titles = Titelslijst.Where(t => t.ID_Artiest == itemDezeWeek.ID_Artiest);
    if (titles.Any())
        id = titles.Max(t => t.ID_Titel) + 1;
}

titel.ID_Titel = id;

答案 2 :(得分:-1)

更短,更正确:

#include <ncurses.h>

#define WIDTH 80
#define HEIGHT 24

WINDOW * mainWin;
WINDOW * sideWin;

int main()
{
    int mainwinStartX = 0;
    int mainwinStartY = 0;
    int mainwinWidth;
    int mainwinHeight;
    int sidewinStartX;
    int sidewinStartY = 0;
    int sidewinWidth;
    int sidewinHeight;

    initscr();
    noecho();
    refresh();
    // Prepairing mainWin sizes
    mainwinWidth = float(2)/3 * WIDTH;
    mainwinHeight = HEIGHT;
    // Prepairing side win sizes
    sidewinStartX = mainwinWidth;
    sidewinWidth = WIDTH - mainwinWidth;
    sidewinHeight = HEIGHT;
    // creating window objects
    mainWin = newwin(mainwinHeight, mainwinWidth, mainwinStartY, mainwinStartX);
    sideWin = newwin(sidewinHeight, sidewinWidth, sidewinStartY, sidewinStartX);
    box(mainWin, 0, 0);
    box(sideWin, 0, 0);
    wrefresh(mainWin);
    wrefresh(sideWin);
    while (TRUE)
    {
        int input = getch();
        if (input == 'x')
            break;
        if (input == KEY_RESIZE)
        {
                refresh();
                delwin(mainWin);
                delwin(sideWin);
                mainWin = newwin(mainwinHeight, mainwinWidth, mainwinStartY, mainwinStartX);
                sideWin = newwin(sidewinHeight, sidewinWidth, sidewinStartY, sidewinStartX);
                box(mainWin, 0, 0);
                box(sideWin, 0, 0);
                wrefresh(mainWin);
                wrefresh(sideWin);

        }
    }
    endwin();
    return 0;
}
相关问题