我被卡住了,希望你能帮助我。
如何写
select table1.*, table2.column1 as name
在linq吗?
我试着:
select new { table1, name = table2.column1 })
但是它的输出是分开的,例如:
table1:{address:valueOfAddress, religion:valueOfReligion, columnN:valueofColumnN}, name: valueOfName
我希望输出如下:
address:valueOfAddress, religion:valueOfReligion, columnN:valueofColumnN, name: valueOfName
谢谢。
我认为解决方案是这样的,
var result = from t1 in table1
from t2 in table2
where condition // if there is any condition to fetch records
select new { address = t1.address, religion= t1.religion,columnN = t1.columnN, Name = t2.name };
JsonConvert.SerializeObject ()使用linq将新创建的对象序列化为json并解决您所面临的问题。我希望你喜欢我的回答由于
class Employee
{
public string employeeID;
public string Name;
public string eventName;
public Employee(string eID, string eName, string eEvents)
{
this.employeeID = eID;
this.Name = eName;
this.eventName = eEvents;
}
}
class Event
{
public int id { get; set; }
public string name { get; set; }
public Event(int _id,string _name)
{
id = _id;
name = _name;
}
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee("PALI_e1", "Parvez Ali", "FOOTBALL"));
employees.Add(new Employee("AALI_e2", "Ashik Ali", "FOOTBALL"));
employees.Add(new Employee("AALI_e3", "Aftab Ali", "CHESS"));
employees.Add(new Employee("AALI_e4", "Arif Ali", "CRICKET"));
List<Event> courses = new List<Event>();
courses.Add(new Event(1,"FOOTBALL"));
courses.Add(new Event(2,"FOOTBALL"));
var result = from l1 in employees
from l2 in courses
where l1.eventName == l2.name
select new { l1, l2.name };
string output = JsonConvert.SerializeObject(result);
Console.WriteLine(output);
}
}
这是预期的输出截图