繁体   English   中英

复杂的LINQ to SQL查询返回列表<T>

[英]Complex LINQ to SQL query to return List<T>

我正在写一个WP 8,它将显示从WCF服务返回的列表。 我想从数据库中选择所有教师,然后从创建的列表中返回另一位教师,该教师将显示5名教师的最大收入( maxInc )最接近我的教师( MyMaxInc )。 例如,如果MyMaxInc = 200而我的用户为225,用户2为240,用户3为210,则返回的列表将按以下顺序显示教师:用户3,用户,用户2。 我的服务实现:

public IEnumerable<mTeachers> GetStuffList(int MyMaxInc, int MyMinInc)
    {
        List<mTeachers> stuffList = new List<mTeachers>();
        DataClasses1DataContext data = new DataClasses1DataContext();
        int inc = 0;
        List<mTeachers> finalList = new List<mTeachers>();
            foreach (var d in data.Stuffs)
            {
                if (d.stuffJob == "teacher") 
                {                   
                    stuffList.Add(new mTeachers(d.stuffName, (int)d.maxInc, (int)d.minInc, "teacher", inc)); inc++;       
                }
            }
            if (inc > 0) 
            {
                foreach (mTeachers element in stuffList)             
                    if () {/*didn't finish this bit because 
                         don't know how to create second list*/ } 
                return finalList;
            }
            else return null;           
    }

服务界面:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    IEnumerable<mTeachers> GetStuffList(int MyMaxInc, int MyMinInc);
    }
[DataContract]
public class mTeachers
{
    [DataMember]
    public string Name;
    [DataMember]
    public int maxInc;
    [DataMember]
    public int minInc;
    [DataMember]
    public string Job;
    [DataMember]
    public int Number;

    public mTeachers(string Name, int maxInc, int minInc, string Job, int Number)
    {
        this.maxInc = maxInc;
        this.minInc = minInc;
        this.Name = Name;
        this.Job = Job;
        this.Number = Number;
    }
}

我还创建了mTeachers类的Number成员, finalList稍后将finalList 例如,当我在循环中创建列表时可以分配一些数字,并使用private static List<Group<T>> GetItemGroups<T>(IEnumerable<T> itemList, Func<T, string> getKeyFunc)方法。

更换

           foreach (mTeachers element in stuffList)             
                if () {/*didn't finish this bit because 
                     don't know how to create second list*/ } 

finalList.AddRange(
    stuffList.OrderBy(x => Math.Abs(x.maxInc - MyMaxInc)).Take(5));

Math.Abs(...)将负差转换为正,以便能够同时比较较小和较大的值。

OrderByTake(5)结合使用Take(5)最小差异排序的前5个元素。

暂无
暂无

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

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