C# 或条件上的Linq到SQL帮助
我有两张桌子: 公司 注意:C# 或条件上的Linq到SQL帮助,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我有两张桌子: 公司 注意:AliasList值用分号分隔 雇员 EmplID | EmployerName ------------------------------------------------------- 1 | Walmart 我需要将员工姓名与公司名称进行比较。如果名称匹配,则获取一些值。否则,将EmployerName与AliasList进行比较,并获得一些值。我需要一些帮助在Linq或linqtosql中获得这项工作。我尝试的解决方案是: var al
AliasList
值用分号分隔
雇员
EmplID | EmployerName
-------------------------------------------------------
1 | Walmart
我需要将员工姓名与公司名称进行比较。如果名称匹配,则获取一些值。否则,将EmployerName与AliasList进行比较,并获得一些值。我需要一些帮助在Linq
或linqtosql
中获得这项工作。我尝试的解决方案是:
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())
首先,您需要为这两个表生成
列表
。然后可以使用Linq
获得结果。我已经创建了一个,下面是代码示例
代码:
使用系统;
使用System.Linq;
使用System.Collections.Generic;
公共课程
{
阶级公司
{
公共字符串CompanyID{get;set;}
公共字符串CompanyName{get;set;}
公共字符串别名列表{get;set;}
}
阶级雇员
{
公共字符串EmplID{get;set;}
公共字符串EmployerName{get;set;}
}
公共静态void Main()
{
案例1();
案例2();
案例3();
}
静态无效案例1()
{
//公司名称==员工名称
List listEmpl=new List(){new Empl(){emplyid=“1”,EmployerName=“Walmart”};
List List Company=new List(){new Company(){CompanyID=“1”,CompanyName=“沃尔玛”,AliasList=“沃尔玛;沃尔玛;Samsclub”};
var结果=来自ListMPL中的e
从listCompany.Where(w=>w.CompanyName.Trim()==e.EmployerName.Trim()| | w.AliasList.Split(“;”)。包含(e.EmployerName.Trim())
选择新的{c.CompanyName,e.EmployerName};
if(result.FirstOrDefault()!=null)
{
Console.WriteLine(result.FirstOrDefault().CompanyName);
}
其他的
{
WriteLine(“结果为空!”);
}
}
静态无效案例2()
{
//别名列表包含EmployerName
List listEmpl=new List(){new Empl(){employId=“1”,EmployerName=“沃尔玛”};
List List Company=new List(){new Company(){CompanyID=“1”,CompanyName=“沃尔玛”,AliasList=“沃尔玛;沃尔玛;Samsclub”};
var结果=来自ListMPL中的e
从listCompany.Where(w=>w.CompanyName.Trim()==e.EmployerName.Trim()| | w.AliasList.Split(“;”)。包含(e.EmployerName.Trim())
选择新的{c.CompanyName,e.EmployerName};
if(result.FirstOrDefault()!=null)
{
Console.WriteLine(result.FirstOrDefault().CompanyName);
}
其他的
{
WriteLine(“结果为空!”);
}
}
静态无效案例3()
{
//不符合任何条件
List listEmpl=new List(){new Empl(){employId=“1”,EmployerName=“沃尔玛”};
List List Company=new List(){new Company(){CompanyID=“1”,CompanyName=“沃尔玛”,AliasList=“沃尔玛;沃尔玛;Samsclub”};
var结果=来自ListMPL中的e
从listCompany.Where(w=>w.CompanyName.Trim()==e.EmployerName.Trim()| | w.AliasList.Split(“;”)。包含(e.EmployerName.Trim())
选择新的{c.CompanyName,e.EmployerName};
if(result.FirstOrDefault()!=null)
{
Console.WriteLine(result.FirstOrDefault().CompanyName);
}
其他的
{
WriteLine(“结果为空!”);
}
}
}
如果可能,我建议更改数据库设计,将别名放在单独的表中(每个别名一行),并将一对多链接放回公司表。谢谢csharpbd。建议的解决方案非常详细,非常有用。
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())
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!");
}
}
}