代码之家  ›  专栏  ›  技术社区  ›  Ash M

如何在linq或sql中编写以下内容

  •  1
  • Ash M  · 技术社区  · 17 年前

    如果我能得到一些帮助来编写一个linq,它将从表a中获取所有字段,以及profile 1在表ab中有值的字段,请显示该值,否则如果profile 1在表ab中没有条目,请将值显示为空。

    表A

    AID    Field
    -----------
    1      OneField
    2      TwoField
    3      ThreeField
    

    表B

    BID    Value
    -----------
    1      OneValue
    2      TwoValue
    3      ThreeValue
    

    表ab

    ABID   AID   BID  ProfileId
    -------------------------
    1      1     1    1
    2      2     3    1
    

    我正在尝试编写一个linq(或sql查询),它将显示配置文件id 1、表a中的所有值以及表b中的值。

    例如

    ProfileID  AID       Field        BID       Value
    --------------------------------------------------------
    1          1         OneField     1         OneValue
    1          2         TwoField     3         ThreeValue
    1          3         ThreeField   NULL      NULL
    

    如您所见,目的是从表A中获取所有字段,并且概要文件1在表AB中有条目的字段显示表B中的值,否则,如果概要文件1在表AB中没有条目,则显示为空。

    如果解决方案需要它,我很乐意用sql编写并通过存储过程调用它。

    谢谢

    6 回复  |  直到 9 年前
        1
  •  0
  •   7wp    17 年前

    我得到了一个工作的linq解决方案和一个完整的自包含测试。我没有在结尾处添加任何断言,但是结尾处的变量“queryresult”将包含一个列表,其中包含您想要的结果。我对它进行了测试并验证了它的有效性。代码如下:

    编辑:针对您的评论,这里是示例2

        public class tablea
        {
            public int AID { get; set; }
            public string Field { get; set; }
        }
        public class tableb
        {
            public int BID { get; set; }
            public string Value { get; set; }
        }
        public class tableab
        {
            public int ProfileID { get; set; }
            public int AID { get; set; }
            public string Field { get; set; }
            public int BID { get; set; }
            public string Value { get; set; }
        }
    
        public class result
        {
            public int? profileid { get; set; }
            public int? aid { get; set; }
            public string field { get; set; }
            public string bid { get; set; }
            public string value { get; set; }
        }
    
        [Test]
        public void TestQuery()
        {
            var tablea = new List<tablea>();
            var tableb = new List<tableb>();
            var tableab = new List<tableab>();
    
            tablea.Add(new tablea{AID = 1,Field = "OneField"});
            tablea.Add(new tablea{AID = 2,Field = "TwoField"});
            tablea.Add(new tablea{AID = 3,Field = "ThreeField"});
    
            tableb.Add(new tableb{BID = 1,Value = "OneValue"});
            tableb.Add(new tableb{BID = 2,Value = "TwoValue"});
            tableb.Add(new tableb{BID = 3,Value = "ThreeValue"});
    
            tableab.Add(new tableab{AID = 1,BID=1,ProfileID = 1});
            tableab.Add(new tableab{AID = 2,BID=3,ProfileID = 1});
    
            var profileId = 1;
    
            var q1 = (from a in tablea
                      let bid = (from ab in tableab where ab.ProfileID == profileId && ab.AID == a.AID select ab.BID).FirstOrDefault()
                      let value = (from ab in tableab where ab.ProfileID == profileId && ab.AID == a.AID && ab.BID == bid select ab.Value).FirstOrDefault()
                    select new result
                                {
                                    profileid = profileId,
                                    aid = a.AID,
                                    field = a.Field,
                                    bid = (bid == 0 ? "null" : bid.ToString()),
                                    value = value ?? "null"
                               }).ToList();
    
        }
    
        2
  •  0
  •   ba__friend    17 年前

    这应该有效(未经测试):

    SELECT ProfileID, A.AID, Field, B.BID, Value 
    FROM AB 
    LEFT OUTER JOIN A ON AB.AID = A.AID
    LEFT OUTER JOIN B ON AB.BID = B.BID
    
        3
  •  0
  •   marc_s MisterSmith    17 年前

    由于需要表A中的所有行,因此需要从表A开始:

    SELECT AB.ProfileID, A.AID, A.Field, B.BID, B.Value 
    FROM A 
    LEFT OUTER JOIN AB ON AB.AID = A.AID
    LEFT OUTER JOIN B ON AB.BID = B.BID
    WHERE (AB.ProfileID = 1 OR AB.ProfileID IS NULL)
    

    如果在查询中从表AB开始,您将只能得到表AB中的内容—并且第3行在表AB中没有条目。

    马克

        4
  •  0
  •   Edwards    17 年前

    [代码] var result=来自 把ab加入到a上。aid等于ab。aid进入tmp1 在b上加入b,id等于ab,bid为tmp2 从tmp1.defaultifempty()/*中的c获取空值*/ 来自tmp2.defaultifempty()中的d 选择新 { C. A. A.场
    C.BID, D.值 }; [代码]

        5
  •  0
  •   nickd    17 年前

    SQL版本如下

    select TableA.*, TableB.*
    from TableA
        left outer join TableC on TableA.ID = Aid
        left outer join TableB on TableB.id = Bid
    where
        TableC.ProfileID = 1
        or TableC.ProfileID is null
    

    外部连接确保即使在连接的另一端没有匹配的记录,也可以从tablea获得结果。因此,还必须在profileid中允许空值。

        6
  •  0
  •   C B dkretz    9 年前

    请参见下面的示例:

    var result = from a  in a
                 join ab in ab on a.aid equals ab.aid into tmp1
             join b  in b  on b.id  equals ab.bid into tmp2
             from c in tmp1.DefaultIfEmpty()  /* this is to get the null values */
             from d in tmp2.DefaultIfEmpty()
             select new 
            {
             c.ProfileID,
             a.AID,
             a.Field,   
                     c.bid,
             d.Value
            };