Linq根据结果中的字段显示不同的行

时间:2016-08-24 16:30:51

标签: c# linq asp.net-mvc-4 linq-to-sql

我正在尝试过滤下面的结果,只根据select中返回的MacAddress字段显示不同的行。这仅在返回单个值时有效,但在返回对象时无效。有什么想法吗?

 // Get all records which have not been imported yet?
        var query = (from s in db.Stagings
            join t in db.Printers on s.MacAddress equals t.MacAddress into tj
            from t in tj.DefaultIfEmpty()
            where t == null
            select new { s.MacAddress,s.PrinterName,s.HostName,s.IPAddress,s.ReportDate}).Distinct();

是否可以在不进行循环的情况下进一步过滤?

1 个答案:

答案 0 :(得分:0)

这会有用吗?

var query = (from s in db.Stagings
                join t in db.Printers on s.MacAddress equals t.MacAddress into tj
                from t in tj.DefaultIfEmpty()
                where t == null
                select new { s.MacAddress,
                            s.PrinterName,
                            s.HostName,
                            s.IPAddress,
                            s.ReportDate
                        }
                ).GroupBy(s => s.MacAddress)
                .Select(s => s.First());