Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示不相关表的组合结果(linq到sql)_C#_Sql_Linq To Sql_Stored Procedures_Multiple Tables - Fatal编程技术网

C# 显示不相关表的组合结果(linq到sql)

C# 显示不相关表的组合结果(linq到sql),c#,sql,linq-to-sql,stored-procedures,multiple-tables,C#,Sql,Linq To Sql,Stored Procedures,Multiple Tables,我正在努力找出使用linq to sql C从两个不相关的表中显示组合结果的最佳方法。这些表是EmailAlerts和TextAlerts,每个表都有用户名、类型和状态以及其他不同的列,但此查询只需要这三个列。出于报告目的,我需要获取系统中有活动警报的用户的快照 示例表: EmailAlerts UserName Type Status Alice 1 0 Alice 1 0 Alice 1 1 Alice 2 0 Al

我正在努力找出使用linq to sql C从两个不相关的表中显示组合结果的最佳方法。这些表是EmailAlerts和TextAlerts,每个表都有用户名、类型和状态以及其他不同的列,但此查询只需要这三个列。出于报告目的,我需要获取系统中有活动警报的用户的快照

示例表:

EmailAlerts
UserName    Type    Status
Alice       1   0
Alice       1   0
Alice       1   1
Alice       2   0
Alice       2   1
Alice       2   1
Bob         1   1
Bob         2   1
Mallory     1   1
Mallory     2   1

TextAlerts
UserName    Type    Status
Alice       1   1
Alice       2   0
Alice       2   1
Bob         1   0
Mallory     1   1
Mallory     2   1
这将被放入csv文件中,示例表的最终结果如下所示:

Username, ActiveType1Email, ActiveType2Email, ActiveType1Text, ActiveType2Text
Alice, Yes, Yes, No, Yes
Bob, No, No, Yes, No
因此,对于每个唯一的用户,找出他们是否有活动状态=0的电子邮件或任何类型的文本警报。对于这两种类型,它们可以有多个警报。用户存储在Sitecore中,因此没有用户表

目前,我首先获取所有唯一的用户名,然后遍历每个用户名,找出它们有哪些警报。它是有效的,但它非常可怕,所以我想找到一个更好的解决方案。可以在一个查询中完成所有操作吗?存储过程是一种更好的方法吗?如果有人能给我指出正确的方向,我可以自己尝试找出代码,我只是不确定解决它的最佳方法

更新:以下是当前的丑陋代码:

public static List<Dictionary<string, string>> GetUserAlertsForCSV()
{
    List<Dictionary<string, string>> alerts = new List<Dictionary<string, string>>();

    var usernames = ((from e in db.EmailAlerts select e.UserName).Union
                    (from t in db.TextAlerts select t.UserName)).Distinct();

    foreach (var username in usernames)
    {
        Dictionary<string, string> d = new Dictionary<string, string>();
        d.Add("username", username);
        bool hasActiveAlert = false;

        var activeType1Email = (from e in db.EmailAlerts
                    join a in db.AlertStatusCodes on e.Status equals a.StatusCode
                    where e.UserName == username
                    && e.Type == (int)AlertType.Type1
                    && a.Description == "active"
                    select e).FirstOrDefault();

        if (activeType1Email != null)
        {
            d.Add("type1email", "Yes");
            hasActiveAlert = true;
        }
        else
        {
            d.Add("type1email", "No");
        }

        // repeat the above for activeType1Text, activeType2Email and activeType2Text

        if (hasActiveAlert)
        {
            alerts.Add(d);
        }
    }

    return alerts;
}
谢谢


安妮莉

试试这个,如果它能给你一个想法的话。我的想法是使用一种新的匿名类型的相关子查询来存储您需要的所有信息。 请查看:

var usernames = ((from e in db.EmailAlerts select e.UserName).Union
                (from t in db.TextAlerts select t.UserName)).Distinct();
var result =
    from u in usernames
    select new 
    {
        Username = u.Username,
        ActiveType1Email = (from e in db.EmailAlerts
                           where e.UserName == u
                           && e.Type == (int)AlertType.Type1
                           && a.Description == "active"
                           select e).FirstOrDefault();
        /*
         ... and so on repeat for activeType1Text, activeType2Email and activeType2Text

        */
    }

    // and then go trough the result set which is IEnumarable<T> and use it for what you need
    foreach(var a in result)
    { 
      var something = a.ActiveType1Email;
      /* etc. */ 
    }

这对您有帮助吗?

对不起,我不清楚,将用示例表更新我的问题。用户不必在两个表中都有记录。这看起来更好,我一直在尝试将其转换为一个或两个大查询,但这可能是最好的解决方法。谢谢你的帮助!你能告诉我们你想要达到的目标的代码吗?也许它会给我们一个提示?这样目标就有点不明确了。@bojanskr-添加的示例表,也将添加到我到目前为止的linq到sql代码中,但请注意,它有味道!:没问题,我只想帮助,不想批评:我已经纠正了解决方案,请查看下面编辑的答案