locked
LINQ with if else RRS feed

  • Question

  • Can the following be translated into LINQ?

    private List<IFileContentItem> content = new List<IFileContentItem>();
    if (item.IsLink())
    {
    	content.Add(new LinkedFile(
            	item, 
                    new DirectoryInfo(Path.Combine(
                    	item.FullName.Replace(
                           	this.source.FullName, 
                           	this.target.FullName
                            )))));
    }
    else
    {
        	content.Add(new LocalFile(
           		item,
               	new DirectoryInfo(Path.Combine(
                   		item.FullName.Replace(
                  		this.source.FullName,
                          	this.target.FullName
                       	)))));
    }

    • Moved by Lisa Zhu Monday, November 12, 2012 7:14 AM linq related (From:Visual C# General)
    Thursday, November 8, 2012 5:36 PM

Answers

  • Given your new code, I'd recommend making a method:

    private IFileContentItem CreateContentItem(FileInfo item)
    {
        var di = new DirectoryInfo(Path.Combine(
                         item.FullName.Replace(
                           	this.source.FullName, 
                           	this.target.FullName)));
        return item.IsLink() 
                  ? (IFileContentItem)new LinkedFile(item, di) 
                  : new LocalFile(item, di));
    }

    You could then write your loop as:

    List<IFileContentItem> content = 
                  this.source
                      .GetFiles("*", SearchOption.AllDirectories)
                      .Select(fi => CreateContentItem(fi))
                      .ToList();


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by servy42 Thursday, November 8, 2012 6:01 PM
    • Marked as answer by Alexander Sun Thursday, November 22, 2012 6:59 AM
    Thursday, November 8, 2012 5:53 PM
  • Well, there is no "loop" here to turn into a query.  You could change this to use a conditional statement, however, if you needed to.  I'd personally write this as:

    var di = new DirectoryInfo(Path.Combine(
                         item.FullName.Replace(
                           	this.source.FullName, 
                           	this.target.FullName)));
    content.Add(item.IsLink() 
                  ? (IFileContentItem)new LinkedFile(item, di) 
                  : new LocalFile(item, di));


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    • Edited by Reed Copsey, JrMVP Thursday, November 8, 2012 5:41 PM
    • Proposed as answer by servy42 Thursday, November 8, 2012 6:00 PM
    • Marked as answer by Alexander Sun Thursday, November 22, 2012 6:59 AM
    Thursday, November 8, 2012 5:40 PM

All replies

  • Well, there is no "loop" here to turn into a query.  You could change this to use a conditional statement, however, if you needed to.  I'd personally write this as:

    var di = new DirectoryInfo(Path.Combine(
                         item.FullName.Replace(
                           	this.source.FullName, 
                           	this.target.FullName)));
    content.Add(item.IsLink() 
                  ? (IFileContentItem)new LinkedFile(item, di) 
                  : new LocalFile(item, di));


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    • Edited by Reed Copsey, JrMVP Thursday, November 8, 2012 5:41 PM
    • Proposed as answer by servy42 Thursday, November 8, 2012 6:00 PM
    • Marked as answer by Alexander Sun Thursday, November 22, 2012 6:59 AM
    Thursday, November 8, 2012 5:40 PM
  • private List<IFileContentItem> content = new List<IFileContentItem>();
    foreach (FileInfo item in this.source.GetFiles("*", 	SearchOption.AllDirectories))
    {
    	if (item.IsLink())
           	{
             	content.Add(new LinkedFile(
                     	item, 
                           	new DirectoryInfo(Path.Combine(
                            	item.FullName.Replace(
                                  	this.source.FullName, 
                                   	this.target.FullName
                            )))));
          	}
         	else
           	{
            	content.Add(new LocalFile(
                  		item,
                        	new DirectoryInfo(Path.Combine(
                         		item.FullName.Replace(
                                 	this.source.FullName,
                                  	this.target.FullName
                            )))));
        	}
    }

    I apologize.  I did not give the entire code.

    Thursday, November 8, 2012 5:49 PM
  • by the way, I really like your code.  Its quite elegant.
    Thursday, November 8, 2012 5:49 PM
  • Given your new code, I'd recommend making a method:

    private IFileContentItem CreateContentItem(FileInfo item)
    {
        var di = new DirectoryInfo(Path.Combine(
                         item.FullName.Replace(
                           	this.source.FullName, 
                           	this.target.FullName)));
        return item.IsLink() 
                  ? (IFileContentItem)new LinkedFile(item, di) 
                  : new LocalFile(item, di));
    }

    You could then write your loop as:

    List<IFileContentItem> content = 
                  this.source
                      .GetFiles("*", SearchOption.AllDirectories)
                      .Select(fi => CreateContentItem(fi))
                      .ToList();


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by servy42 Thursday, November 8, 2012 6:01 PM
    • Marked as answer by Alexander Sun Thursday, November 22, 2012 6:59 AM
    Thursday, November 8, 2012 5:53 PM
  • Hi Newmanb1,

    From your description, I ‘d like to move this post to  LINQ Project General forum for better support.

    Thanks for your understanding.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, November 12, 2012 7:14 AM