C# 使用Linq选择不同的行

C# 使用Linq选择不同的行,c#,sql,linq,C#,Sql,Linq,数据如下 ID Title Category About Link CategoryID 1 The Matrix Sci-Fi Text goes here http://... 1 2 The Simpsons Cartoon Text goes here http://... 2 3 Avengers Action Text goes here http://... 3 4 The Matrix

数据如下

ID Title        Category About           Link        CategoryID
1  The Matrix   Sci-Fi   Text goes here  http://...  1 
2  The Simpsons Cartoon  Text goes here  http://...  2
3  Avengers     Action   Text goes here  http://...  3
4  The Matrix   Sci-Fi   Text goes here  http://...  1
5  The One      Sci-Fi   Text goes here  http://...  1
6  The Hobbit   Sci-Fi   Text goes here  http://...  1
我有一个包含类别的复选框列表。问题是,如果用户选择“动作”和“科幻”作为类别来显示矩阵,矩阵将显示两次

这是我在SQL查询中获取唯一行的尝试

select distinct title, about, link from mytable
inner join tableCategories on categoryID = tableCategoriesID
group by title, about, link
使用LINQ

(from table in movieTables
join x in categoryIDList
on categoryID equals x
slect table).Distinct()
请注意,类别位于由categoryID链接的单独表格中。
需要帮助在LINQ中显示唯一或不同的行。

您可以愉快地将结果选择到所需内容的列表中:

var v = from entry in tables
        where matching_logic_here
        select new {id = some_id, val=some_value};
然后,您可以根据需要在该列表上运行distinct(当然,上面的
ToList()
将使其成为一个)

下面应该说明我的意思(只需粘贴到linqpad中即可。如果您使用的是VS,请去掉
.Dump()

将产生:

1矩阵科幻小说
3复仇者行动
5一部科幻小说
6霍比特科幻小说

void Main()
{
    var input  = new List<mock_entry> {
        new mock_entry {id = 1, name="The Matrix", cat= "Sci-Fi"},
        new mock_entry {id = 2, name="The Simpsons" ,cat= "Cartoon"},
        new mock_entry {id = 3, name="Avengers" ,cat= "Action"},
        new mock_entry {id = 4, name="The Matrix", cat= "Sci-Fi"},
        new mock_entry {id = 5, name="The One" ,cat= "Sci-Fi"},
        new mock_entry {id = 6, name="The Hobbit",cat= "Sci-Fi"},
    };

    var v = input.Where(e=>e.cat == "Action" || e.cat =="Sci-Fi")
                .Dump()
                .Select(e => new {n = e.name, c =e.cat})
                .Dump()
    ;

    var d = v.Distinct()
            .Dump()
    ;
}

// Define other methods and classes here
public struct mock_entry {
    public int id {get;set;}
    public string name {get;set;}
    public string cat {get;set;}
}
var v = input.GroupBy (i => i.name)
   .Select(e => e.First ())
   .Dump()
   .Where(e=>e.cat == "Action" || e.cat =="Sci-Fi")
   .Dump()
;