0

I don't know that it is possible or not. i have search some links but didn't find the solution.

i have a linq query for getting some data from the database. Now, In my Database in the table column Photo can have or can not have a Image. it means Photo can be null also.

so My Question is: is there a way rather than use if else condition in linq query that we can put my code like this:

DataTable Customer=ds.Tables["Customer"]
    if (Photo==null)
    {
        var items = (from d in Customer.AsEnumerable()
                                 where d.Field<string>("strHin") != string.Empty
                                 orderby d.Field<DateTime>("date")
                                 select new News
                                 {
                                     NewsItemId = d.Field<Int32>("intCustId").ToString(),
                                     HeadLine = d.Field<string>("strHin"),
                                     Photo = null,
                                 }).Take(Convert.ToInt32(limit)).ToList();
                    return items;

    }
    if(Photo !=null)
    {
        var items = (from d in Customer.AsEnumerable()
                                 where d.Field<string>("strHin") != string.Empty
                                 orderby d.Field<DateTime>("date")
                                 select new News
                                 {
                                     NewsItemId = d.Field<Int32>("intCustId").ToString(),
                                     HeadLine = d.Field<string>("strHin"),
                                     Photo = @"http://192.168.1.12:801/ImageById/" + d.Field<Int32>("intCustId") + ".jpg"
                                 }).Take(Convert.ToInt32(limit)).ToList();
                    return items;
    }

i don't want to use if else because i have already used many if else in my code. is there a way that i can use this with linq query without repeatation of code.

7
  • 7
    Your making us play spot the difference... what is the difference between your two code blocks? Commented Sep 14, 2012 at 7:12
  • Right now, Im struggling to see the differnce in your 2 linq statements.. Commented Sep 14, 2012 at 7:12
  • 1
    and theres nothing wrong with elses! Commented Sep 14, 2012 at 7:13
  • this code does work? It doesn't look to me. First, write code that actually works. Then think about refactoring. Commented Sep 14, 2012 at 7:13
  • sorry guys i hav edited my question Commented Sep 14, 2012 at 7:16

1 Answer 1

1
var result=    (from d in Customer.AsEnumerable()
                             where d.Field<string>("strHin") != string.Empty
                             orderby d.Field<DateTime>("date")
                             select new News
                             {
                                 NewsItemId = d.Field<Int32>("intCustId").ToString(),
                                 HeadLine = d.Field<string>("strHin"),
                                 Photos = Photo==null?"":@"http://192.168.1.12:801/ImageById/" + d.Field<Int32>("intCustId") + ".jpg",
                             }).Take(Convert.ToInt32(limit)).ToList();
                return items;

Note: i have change variable name of anonymous from Photo to Photos coz its conflicting with outer Photo variable with inner anonymous type variable Photo

Sign up to request clarification or add additional context in comments.

1 Comment

I do not even think you actually have to rename the News property/variable Photo (other than for readability). I believe the compiler is able to use the right Photo from the context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.