我有两张桌子:
公司
注意:AliasList
值由分号
CompanyID | CompanyName | AliasList
-------------------------------------------------------
1 | Wal-mart | Walmart;Wal Mart;Samsclub
EMPL
EmplID | EmployerName
-------------------------------------------------------
1 | Walmart
我需要将EmployerName与CompanyName进行比较。如果名称匹配,则获取一些值。否则,将EmployerName与AliasList进行比较并获取一些值。我需要一些帮助才能在Linq
或Linq To SQL
中完成这项工作。我试过的解决方案:
var allAliasListData = getting all the AliasList values into a List by using split() function.
var result = from e in Empl
from c in Company.Where(w => w.Company.Trim() == r.EmployerName.Trim() || allAliasListData.Contains(r.EmployerName.Trim())
答案 0 :(得分:0)
首先,您需要为两个表生成List
。然后,您可以使用Linq
获取结果。我创建了example in DotNetFiddle,代码示例如下。
CODE:
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
class Company
{
public string CompanyID { get; set; }
public string CompanyName { get; set; }
public string AliasList { get; set; }
}
class Empl
{
public string EmplID { get; set; }
public string EmployerName { get; set; }
}
public static void Main()
{
Case1();
Case2();
Case3();
}
static void Case1()
{
//CompanyName == EmployerName
List<Empl> listEmpl = new List<Empl>() { new Empl() { EmplID = "1", EmployerName = "Walmart" } };
List<Company> listCompany = new List<Company>() { new Company() { CompanyID = "1", CompanyName = "Walmart", AliasList = "Wal-mart;Wal Mart;Samsclub" } };
var result = from e in listEmpl
from c in listCompany.Where(w => w.CompanyName.Trim() == e.EmployerName.Trim() || w.AliasList.Split(';').Contains(e.EmployerName.Trim()))
select new { c.CompanyName, e.EmployerName };
if(result.FirstOrDefault() != null)
{
Console.WriteLine(result.FirstOrDefault().CompanyName);
}
else
{
Console.WriteLine("Result is empty!");
}
}
static void Case2()
{
//AliasList Contain EmployerName
List<Empl> listEmpl = new List<Empl>() { new Empl() { EmplID = "1", EmployerName = "Wal-mart" } };
List<Company> listCompany = new List<Company>() { new Company() { CompanyID = "1", CompanyName = "Walmart", AliasList = "Wal-mart;Wal Mart;Samsclub" } };
var result = from e in listEmpl
from c in listCompany.Where(w => w.CompanyName.Trim() == e.EmployerName.Trim() || w.AliasList.Split(';').Contains(e.EmployerName.Trim()))
select new { c.CompanyName, e.EmployerName };
if(result.FirstOrDefault() != null)
{
Console.WriteLine(result.FirstOrDefault().CompanyName);
}
else
{
Console.WriteLine("Result is empty!");
}
}
static void Case3()
{
//Not match any condition
List<Empl> listEmpl = new List<Empl>() { new Empl() { EmplID = "1", EmployerName = "Wal - mart" } };
List<Company> listCompany = new List<Company>() { new Company() { CompanyID = "1", CompanyName = "Walmart", AliasList = "Wal-mart;Wal Mart;Samsclub" } };
var result = from e in listEmpl
from c in listCompany.Where(w => w.CompanyName.Trim() == e.EmployerName.Trim() || w.AliasList.Split(';').Contains(e.EmployerName.Trim()))
select new { c.CompanyName, e.EmployerName };
if(result.FirstOrDefault() != null)
{
Console.WriteLine(result.FirstOrDefault().CompanyName);
}
else
{
Console.WriteLine("Result is empty!");
}
}
}