简体   繁体   English

使用linq从lastname +“,” + firstname的组合中搜索名字

[英]search firstname from combination of lastname+“, ”+firstname using linq

i have written a stored procedure which returns me Customer Name in below format 我已经编写了一个存储过程,该存储过程以以下格式返回给我客户名称

LastName+", "+FirstName

i have assign it to Class of properties like below. 我已经将其分配给如下所示的属性类。

Customer = DbContext.ExecuteStoreQuery<SearchEmployeeCDTO>("exec GetCustomerDetails").AsQueryable().ToList();

now i want to search the lastName like below i have done for Customer companyName 现在我想搜索lastName,如下所示,我已经为客户companyName做过搜索

if(CompanyName!=null && LastName==null)
Customer = Customer.Where(c => c.CompName.Contains(CompanyName)).ToList();

please suggest how i can search for the LastName from combination of lastName and FirstName format 请建议我如何从lastName和FirstName格式的组合中搜索LastName

thanks, 谢谢,

Maybe this? 也许这个吗?

if(LastName!=null)
  Customer = Customer.Where(c => c.CustName.StartsWith(LastName)).ToList();

if you try to check the values which another list contains the same or not then you can use this : (if you retrieve these as different properties of the generic list element) 如果您尝试检查另一个列表包含的值是否相同,则可以使用以下命令:(如果您将这些作为通用列表元素的不同属性进行检索)

bool b = your1stList.Exists(c=> c.firstName == your2ndList.firstName ,
                               c.middleName == your2ndList.middleName ,
                               c.lastName  == your2ndList.lastName)

if ( b == true) 
{ 
  MessageBox.Show("You already have this Customer"); 
}

this is a good way to retrieve all properties without any concatanation activity..Saves you from some headaches in futural queries ;) 这是在不进行任何合并活动的情况下检索所有属性的好方法。.使您免于将来查询中的麻烦;)

In the other hand, if we continue from your query then you need to get the value of lastname + firstName into a string and use String.Split(",") function to put in a string array then if the word lastName+firstname check the array[0] index for the lastname and array[1] for the firstname . 另一方面,如果我们从您的查询继续进行,则需要将lastname + firstName的值转换为字符串,并使用String.Split(“,”)函数放入字符串数组,然后如果单词lastName + firstname检查姓氏使用array [0]索引,姓氏使用array [1]。

Choose which way you want.. 选择您想要的方式。

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

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