简体   繁体   English

如何从C#列表中的记录中仅获取一个值?

[英]How to get only one value from the record in the list in c#?

I have two custom list: 我有两个自定义列表:

List<Detailsofemployee> empDetails = new List<Detailsofemployee>();
List<regionDetails> regDetails = new List<regionDetails>();
  • empDetails have values {"1","Sam","IT"} . empDetails值为{"1","Sam","IT"}
  • regDetails have values {"IT","UK"} . regDetails值为{"IT","UK"}

I want to get the data from first list and based on that i want to get data from another list 我想从第一个列表中获取数据,并据此从另一个列表中获取数据

var det = (from l in empDetails where l.ID == "1" select l.Department)
var region = (from l in regDetails where l.Department == det select l.Country)

How can I get det = "IT" ? 如何获得det = "IT"

with checking and user input value 带有检查和用户输入值

using System.Linq;

var userInputId = //user input value
var emp = empDetails.FirstOrDefault(x => x.Id == userInputId);
var regDetail = new regionDetails();
if (emp != null)
{
   regDetail =  regDetails.FirstOrDefault(c => c.Department == emp.Department);
}
if (regDetail != null)
{
    return regDetail.Country;
}

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

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