我有一个类AccountCommList它有一个属性Media ,可以是电话,电子邮件,纸张等。
我需要打电话给另一个列表,其中有媒体类型Phone是移动电话还是固定电话(用0或1表示),另一个列表也有一个importance属性,显示他们认为在列表中应该更高的顺序表示联系订购。 这是我的代码。
protected int RetrievePhoneType(string info, string acctNumber) { int type = xPhoneTypeList.Where(p => p.PhoneNum == info && p.AccountNum == acctNumber).Select(p => p.PhoneType).FirstOrDefault(); return type; }此代码选择它是单元格还是固定电话,请记住此PhoneTypeList还包含我需要的importance属性。
这是linq声明。
PhoneTypeListInfo xPhoneTypeList = new PhoneTypeListInfo(); AccountCommList commList = new AccountCommList(acct.AccountNumber, "WEBUSER"); AccountCommList cellPhoneList = commList.Where(x=> x.Media == "Phone").Where(x => 1 == RetrievePhoneType(x.Info, acct.AccountNumber)).OrderByDescending(?????).ToList();我很确定我需要修改RetrievePhoneType方法,但我不确定如何在commList的不同列表上使用linq语句
编辑显示AccountCommList为后代。
public class AccountCommList { public AccountCommList(); public string ContactID { get; set; } public string Info { get; set; } public string Media { get; set; } }编辑2
显示具有重要性属性的PhoneTypeListInfo。
public class PhoneTypeListInfo { public string AccountNum { get; set; } public int PhoneType { get; set; } public string PhoneNum { get; set; } public int Importance { get; set; } }I have a class AccountCommList it has a property Media which could be Phone, Email, Paper, etc.
I need to make a call to another List which has whether the Media type Phone is a mobile or Landline (denoted by a 0 or 1), this other list also has an importance property showing order they perceive should be higher in the list denoting contact order. Here is the code I have.
protected int RetrievePhoneType(string info, string acctNumber) { int type = xPhoneTypeList.Where(p => p.PhoneNum == info && p.AccountNum == acctNumber).Select(p => p.PhoneType).FirstOrDefault(); return type; }This code selects whether it is a cell or landline, keep in mind this PhoneTypeList also contains the importance property I need.
Here is the linq statement.
PhoneTypeListInfo xPhoneTypeList = new PhoneTypeListInfo(); AccountCommList commList = new AccountCommList(acct.AccountNumber, "WEBUSER"); AccountCommList cellPhoneList = commList.Where(x=> x.Media == "Phone").Where(x => 1 == RetrievePhoneType(x.Info, acct.AccountNumber)).OrderByDescending(?????).ToList();I'm pretty sure I need to modify the RetrievePhoneType Method, but I'm not sure how to use a linq statement on a different list within the commList
Edit Showing AccountCommList for posterity.
public class AccountCommList { public AccountCommList(); public string ContactID { get; set; } public string Info { get; set; } public string Media { get; set; } }EDIT 2
Showing PhoneTypeListInfo that has the Importance Property.
public class PhoneTypeListInfo { public string AccountNum { get; set; } public int PhoneType { get; set; } public string PhoneNum { get; set; } public int Importance { get; set; } }最满意答案
将返回类型更改为PhoneTypeListInfo。
protected PhoneTypeListInfo RetrievePhoneType(string info, string acctNumber) { var type = xPhoneTypeList .Where(p => p.PhoneNum == info && p.AccountNum == acctNumber) .FirstOrDefault(); return type; }然后使用LINQ理解语法。 因此,很容易关联集合。
var cellPhoneList = (from x in commList where x.Media == "Phone" let p = RetrievePhoneType(x.Info, acct.AccountNumber) where 1 == p.PhoneType orderby p.Importance descending select x).ToList();无论如何,您的方法总是返回第一个找到的对象。 因此排序是不可能的。
Change the return type to PhoneTypeListInfo.
protected PhoneTypeListInfo RetrievePhoneType(string info, string acctNumber) { var type = xPhoneTypeList .Where(p => p.PhoneNum == info && p.AccountNum == acctNumber) .FirstOrDefault(); return type; }Then use the LINQ comprehension syntax. Thus it is easy to associate collections.
var cellPhoneList = (from x in commList where x.Media == "Phone" let p = RetrievePhoneType(x.Info, acct.AccountNumber) where 1 == p.PhoneType orderby p.Importance descending select x).ToList();Anyway, your method always returns the first found object. So sorting is impossible.
更多推荐
发布评论