简体   繁体   中英

Complex LINQ to SQL query to return List<T>

I'm writing a WP 8 that will show list returned from the WCF service. I want to select all teachers from the database and then from the created list return another one that will show 5 teachers whose maximum income ( maxInc ) is closest to mine ( MyMaxInc ). For example if MyMaxInc = 200 and I have user with 225, user2 with 240 and user3 with 210 the returned list will show teachers in the following order: user3, user, user2. My service implementation:

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;           
    }

Service interface:

[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;
    }
}

Also I created Number member of mTeachers class to group finalList later. For example I can assign some number when I'm creating a list in the loop and the group it with private static List<Group<T>> GetItemGroups<T>(IEnumerable<T> itemList, Func<T, string> getKeyFunc) method.

Replace

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

with

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

Math.Abs(...) converts negative differences to positive to be able to compare smaller and larger values at the same time.

OrderBy combined with Take(5) takes the 5 first elements sorted by the smallest difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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