簡體   English   中英

Linq-to-Entities,比較不同字段/列中的int值並返回最大值

[英]Linq-to-Entities, compare int values in different fields/columns and return max value

我有一個包含8個整數值的記錄的表:

TableChild:
    Parent    Name    int1    int2    int3    int4    int5    int6    int7    int8
    a         w       1       3       2       0       1       3       4       3
    a         x       4       5       2       5       3       2       4       6
    b         y       5       3       5       3       1       1       3       4
    b         z       4       1       2       4       2       2       4       2

例如,我需要找到“ x”的最大值。 例如,我還(另外)需要在整個表中為父級“ a”獲取最大值。

var query = from row in TableChild
            where row.Name == x
            select new 
            {
                Parent = row.Parent,
                Name = row.Name,
                status = GetHighestValueOfRowInTableChild, 
                ...           
            };

返回{(a,x,6)}

var queryParent = from row in tableParent     
                  where row.Name == a
                  select new 
                  {
                      Parent = row.Name,
                      status = GetHighestValueOfAllChildItemsInTableChild,
                      ...
                  };

返回{(a,6)}

我玩過.Max()並嘗試使用表達式,但沒有運氣,因為我可能應該使用多個聯接,但無法弄清楚它們應該如何交互。

使用int {X}屬性的值創建一個數組,稍后獲取該數組的最大值

var queryParent = from row in tableParent     
                      where row.Name == a
                      select new 
                      {

                         Parent = row.Name,
                         status = 
                         (new int[]{row.int1,row.int2,row.int3,row.int4,
                         row.int5,row.int6,row.int7,row.int8}).Max();
                          ...

                      };

除非您要獲取大量記錄並需要減小數據庫響應的大小,否則最好只獲取所有列並在客戶端執行Max

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM