如何在Linq或Sql中编写以下内容

时间:2009-08-30 09:19:12

标签: c# sql linq linq-to-sql tsql

感谢我是否可以帮助编写一个能从表A中获取所有字段的LINQ,以及配置文件1在表AB中具有值的那些字段,显示该值,否则如果配置文件1在表AB中没有条目,然后将值显示为null。

表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中的值。

e.g。

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中没有条目,然后显示为null。

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

由于

6 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

由于你想要表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中的条目。

马克

答案 2 :(得分:0)

[代码] var result = from a in a              在a.aid上将ab加入ab等于ab.aid到tmp1          将b.id等于ab.bid中的b加入到tmp2中          从cmp1.DefaultIfEmpty()/ *中的c来获取空值* /          来自d in tmp2.DefaultIfEmpty()          选择新的         {          c.ProfileID,          a.AID,          a.Field,
                 c.bid,          d.Value         }; [/代码]

答案 3 :(得分:0)

请参阅以下示例:

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
        };

答案 4 :(得分:0)

我得到了一个有完整自包含测试的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();

    }

答案 5 :(得分:0)

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中允许空值。

相关问题