Click here to Skip to main content
15,884,388 members
Home / Discussions / C#
   

C#

 
GeneralRe: "This command is not available because no document is open." Pin
Freak3023-Oct-14 3:56
Freak3023-Oct-14 3:56 
GeneralRe: "This command is not available because no document is open." Pin
Tejas Shastri23-Oct-14 4:09
Tejas Shastri23-Oct-14 4:09 
Questionlaunch another Application from memory by C# Pin
iscreen22-Oct-14 0:15
iscreen22-Oct-14 0:15 
AnswerRe: launch another Application from memory by C# Pin
Richard MacCutchan22-Oct-14 0:20
mveRichard MacCutchan22-Oct-14 0:20 
AnswerRe: launch another Application from memory by C# PinPopular
Richard Deeming22-Oct-14 2:45
mveRichard Deeming22-Oct-14 2:45 
GeneralRe: launch another Application from memory by C# Pin
iscreen22-Oct-14 2:57
iscreen22-Oct-14 2:57 
QuestionCalendar Extender Pin
sadmoh21-Oct-14 23:52
sadmoh21-Oct-14 23:52 
Questionusing a Func with dynamic parameters as a query: pros ? cons ? and a few other miscellaneous questions on Func Pin
BillWoodruff21-Oct-14 13:03
professionalBillWoodruff21-Oct-14 13:03 
Note: the definition of the 'Student class, and the List<Student> 'studentList, used here is appended below.

Suppose we want a query function that, given a list of values-to-match, returns all instances of 'Student in the 'studentList which, for fields that are of Type List<>, have any of the values-to-match ?
C#
Func<Student, List<int>, bool> MatchFromScoresList = (student, itemList)
    => student.Scores.Any(isMatchingEntry => itemList.Contains(isMatchingEntry));

Func<Student, List<string>, bool> MatchFromCommentsList = (student, itemList)
    => (student.Comments != null) && student.Comments.Any(isMatchingEntry
        => itemList.Contains(isMatchingEntry));
Okay, now we can use queries like:
C#
var test1 = students.Where(student => MatchFromScoresList(student, new List<int>{60,91}));

// test for null, since not every Item defines a Comments list
var test2 = students.Where(student => MatchFromCommentsList(student, new List<string> { "one", "two"}));
Question 1: is there any value to writing these function calls using the static 'Where of 'Enumerable ?
C#
var test1 = Enumerable.Where(students, student
    => MatchFromScoresList(student, new List<int>{60,91}));
Question 2: Suppose we wanted to define one Func to handle both matching Lists of Types Int, and String ? Yes: this is an intellectual exercise, probably unnecessary excursion into the impractical and esoteric ... until you tell me it's not ! In fact, I suspect using dynamic in a Func is not a good practice, and would lead to impaired performance, and, since it's easy to chain multiple Linq calls and do 'Joins ... well ... I still want to know Smile | :)

Clearly we can pass use a 'dynamic variable in the Func to get whatever Type of values-to-check List we want into the Func: these will work fine with the same two sample calls shown above:
C#
Func<Student, dynamic, bool> dynMatchFromScoresList = (student, itemList)
    => student.Scores.Any(isMatchingEntry => itemList.Contains);

Func<Student, dynamic, bool> dynMatchFromCommentsList = (student, itemList)
    => (student.Comments != null) && student.Comments.Any(isMatchingEntry
        => itemList.Contains(isMatchingEntry));
But, how we could pass something into the Func that would tell it which Field in the data to compare to: Student.Scores, or Student.Comments ? The only way I can see to do that now is to use a boolean flag:
C#
Func<Student, dynamic, bool, bool> dynMatchFromMultiTypeList = (student, itemList, isListType) =>
    (isListType)
    ?
       // integer list 
       (student.Comments != null) && student.Comments.Any(isMatchingEntry
        => itemList.Contains(isMatchingEntry))
    :
       // string list
       (student.Comments != null) && student.Comments.Any(isMatchingEntry
        => itemList.Contains(isMatchingEntry));

// test
var test0 = Enumerable.Where(students, student => dynMatchFromMultiTypeList(student, new List<int> { 88, 91 }, true));
Is it possible to do some kind of Type checking in the body of the Func: perhaps you can enlighten me, thanks !
public class Student
{
    public string First { get; set; }
    public string Last { get; set; }
    public int ID { get; set; }
    public List<int> Scores;
    public List<string> Comments;
}

private List<Student> studentList;

// initialization
private void initializeStudentList()
{
    // sample data copied from somewhere on one of MSDN's pages on Linq
    studentList = new List<Student>
    {
        new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}, Comments = new List<string>{"one","two"}},
        new Student {First="Claire", Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},
        new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {35, 60, 91, 70}},
        new Student {First="Fadi", Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90, 94}, Comments = new List<string>{"three","two"}},
        new Student {First="Hanying", Last="Feng", ID=117, Scores= new List<int> {93, 92, 80, 87}},
        new Student {First="Michael", Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91} }
    };
}

« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali


modified 21-Oct-14 21:59pm.

AnswerRe: using a Func with dynamic parameters as a query: pros ? cons ? and a few other miscellaneous questions on Func Pin
Richard Deeming22-Oct-14 2:41
mveRichard Deeming22-Oct-14 2:41 
GeneralRe: using a Func with dynamic parameters as a query: pros ? cons ? and a few other miscellaneous questions on Func Pin
BillWoodruff22-Oct-14 6:19
professionalBillWoodruff22-Oct-14 6:19 
GeneralRe: using a Func with dynamic parameters as a query: pros ? cons ? and a few other miscellaneous questions on Func Pin
Richard Deeming22-Oct-14 6:38
mveRichard Deeming22-Oct-14 6:38 
GeneralRe: using a Func with dynamic parameters as a query: pros ? cons ? and a few other miscellaneous questions on Func Pin
BillWoodruff22-Oct-14 7:44
professionalBillWoodruff22-Oct-14 7:44 
Questionspeedtest.net Like result Pin
Jassim Rahma21-Oct-14 10:21
Jassim Rahma21-Oct-14 10:21 
AnswerRe: speedtest.net Like result Pin
Pete O'Hanlon21-Oct-14 10:26
mvePete O'Hanlon21-Oct-14 10:26 
GeneralRe: speedtest.net Like result Pin
Jassim Rahma21-Oct-14 10:31
Jassim Rahma21-Oct-14 10:31 
GeneralRe: speedtest.net Like result Pin
PIEBALDconsult21-Oct-14 10:35
mvePIEBALDconsult21-Oct-14 10:35 
GeneralRe: speedtest.net Like result Pin
Bernhard Hiller22-Oct-14 22:52
Bernhard Hiller22-Oct-14 22:52 
GeneralRe: speedtest.net Like result Pin
PIEBALDconsult21-Oct-14 10:31
mvePIEBALDconsult21-Oct-14 10:31 
Questiontrying to use stored proc @fields in winforms with sql connection. Error procedure or function @Field expects parameter... Pin
Sam 910021-Oct-14 8:34
Sam 910021-Oct-14 8:34 
AnswerRe: trying to use stored proc @fields in winforms with sql connection. Error procedure or function @Field expects parameter... Pin
Richard Deeming21-Oct-14 8:47
mveRichard Deeming21-Oct-14 8:47 
AnswerRe: trying to use stored proc @fields in winforms with sql connection. Error procedure or function @Field expects parameter... Pin
PIEBALDconsult21-Oct-14 9:10
mvePIEBALDconsult21-Oct-14 9:10 
GeneralRe: trying to use stored proc @fields in winforms with sql connection. Error procedure or function @Field expects parameter... Pin
Sam 910021-Oct-14 9:53
Sam 910021-Oct-14 9:53 
GeneralRe: trying to use stored proc @fields in winforms with sql connection. Error procedure or function @Field expects parameter... Pin
PIEBALDconsult21-Oct-14 10:04
mvePIEBALDconsult21-Oct-14 10:04 
GeneralRe: trying to use stored proc @fields in winforms with sql connection. Error procedure or function @Field expects parameter... Pin
Sam 910021-Oct-14 11:38
Sam 910021-Oct-14 11:38 
GeneralRe: trying to use stored proc @fields in winforms with sql connection. Error procedure or function @Field expects parameter... Pin
PIEBALDconsult21-Oct-14 11:48
mvePIEBALDconsult21-Oct-14 11:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.