简体   繁体   English

如何使用linq从另一个表中订购具有匹配ID的数据?

[英]How to order data with matching ID from another table using linq?

So I have 2 tables and I am trying to retrieve the data from table 1, however I need it sorting. 所以我有2个表,我试图从表1中检索数据,但是我需要对它进行排序。 Table 1 contains an ID from table 2 which refers to a string, I need to order the table 1 data by this string in table 2. 表1包含表2中的ID,该ID指向一个字符串,我需要通过表2中的此字符串对表1的数据进行排序。

For example... 例如...

TABLE 1 表格1

   COL_A    |    COL_B   |   COL_C
     1      |      3     |   sample
     2      |      1     |    test
     3      |      2     |   string

TABLE 2 表2

   COL_A    |    COL_B
     1      |    my name
     2      |    his name
     3      |    her name

So I need to order all the data in TABLE 1 by COL_B, but order it by the string in TABLE 2 where the ID matches TABLE 1 COL_B with TABLE 2 COL_A. 因此,我需要按COL_B排序表1中的所有数据,但按ID匹配表1 COL_B和表2 COL_A的表2中的字符串对其进行排序。

How can I do this using LINQ? 如何使用LINQ做到这一点?

You basically need to join two tables, then order by TABLE2.COL_B, then select TABLE1. 您基本上需要连接两个表,然后按TABLE2.COL_B的顺序进行排序,然后选择TABLE1。 Below linq expression should work. 下面的linq表达式应该起作用。

from t1 in TABLE1
join t2 in TABLE2 on t1.COL_B equals t2.COL_A
orderby t2.COL_B
select t1

I have given the solution below 我在下面给出了解决方案

class Table1
{
    public int IdA { get; set; }
    public int IdB { get; set; }
    public string Table1StrValue { get; set; }
}

class Table2
{
    public int IdA { get; set; }
    public string Table2StrValue { get; set; }
}

var table1List = new List<Table1>()
    {
        new Table1 {IdA = 1, IdB = 3, Table1StrValue = "sample"},
        new Table1 {IdA = 2, IdB = 1, Table1StrValue = "test"},
        new Table1 {IdA = 3, IdB = 2, Table1StrValue = "string"},
    };
var table2List = new List<Table2>()
    {
        new Table2 {IdA = 1, Table2StrValue = "my Name"},
        new Table2 {IdA = 2, Table2StrValue = "his Name"},
        new Table2 {IdA = 3, Table2StrValue = "her Name"},
    };

var result = from table2 in table2List
        join table1 in table1List on table2.IdA equals table1.IdA
        orderby table2.Table2StrValue
        select new {table2.IdA, table2.Table2StrValue, table1.Table1StrValue};

So I wrote this, and used Musa to help finish it off.. I think it's what you want 所以我写了这本书,并用Musa帮忙完成了..我认为这就是你想要的

public class table1
{
   public int a;
   public int b;
   public string c;
}

public class table2
{
   public int a;
   public string b;
}

void Main()
{
List<table1> table1 = new List<table1>()
{
    new table1(){ a=1,b=2, c="RAWR"},
    new table1(){ a=2,b=4, c="DERP"},
    new table1(){ a=3,b=1, c="FOO"},
    new table1(){ a=4,b=3, c="BAR"},
};

List<table2> table2 = new List<table2>()
{
    new table2(){a=1,b="A"},
    new table2(){a=2,b="B"},
    new table2(){a=3,b="D"},
    new table2(){a=4,b="C"},
};

var something = from t1 in table1
join t2 in table2 on t1.b equals t2.a
orderby t2.b
select t1;

Console.WriteLine (something);

} }

这可能是最短的方法。

var ret = TABLE1.Join(TABLE2, a => a.COL_A, b => b.COL_A, (a, b) => new {COL_C = b.COL_C)}).OrderBy(s => s.COL_C);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM