使用两个SQL表获取Linq值

时间:2014-11-12 22:29:15

标签: c# sql-server linq

我在编写正确的LINQ语法时遇到了麻烦。

我有一个名为eloLists的表,其值为ID(int)和Rank(字符串),还有一个名为Users的表,其值为elo(int)。

我想通过获取与rank列匹配的相应elo来获取用户的ID

所以我有一行像

Temp = Temp + person.summonerID + "," + person.name + "," + person.summoner + "," + myQuery + ",";

我尝试使用myQuery来获得这个答案。我的尝试就是这样,但我迷路了......

var myQuery = db.eloLists.Select(x => x.rank.Where(x.ID = db.Users.Select(y => y.elo))); 我不确定这是否正确。

编辑:

var myQuery = db.eloLists.Where(x => db.Users.Select(y => y.elo).Contains(x.ID)).Select(e => e.rank);

    foreach (var person in db.Users)
    {
        Temp = Temp + person.summonerID + "," + person.name + "," + person.summoner + "," + myQuery + ",";
    }

1 个答案:

答案 0 :(得分:1)

创建一个返回字符串并接受用户对象的方法。

public string getrank(User person) 
{
    var myQuery = db.eloLists.FirstOrDefault(x => x.ID == person.elo));

    if(myQuery == null) return string.Empty;

    return myQuery.Rank;
} 

然后使用:

foreach (var person in db.Users)
{
    Temp = Temp + person.summonerID + "," + person.name + "," + person.summoner + "," + getrank(person)  + ",";
 }