使用Linq从方法返回值降序排序(Using Linq to Order by Descending from a Method Return value)

编程入门 行业动态 更新时间:2024-10-23 13:29:16
使用Linq从方法返回值降序排序(Using Linq to Order by Descending from a Method Return value)

我有一个类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.

更多推荐

本文发布于:2023-07-30 22:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:返回值   方法   降序   Linq   Return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!