小编JSp*_*ang的帖子

使用LINQ获取一个List <>中的项目,这些项目不在另一个List <>中

我会假设有一个简单的LINQ查询来执行此操作,我只是不确定如何.请参阅下面的代码段.

class Program
{
    static void Main(string[] args)
    {
        List<Person> peopleList1 = new List<Person>();
        peopleList1.Add(new Person() { ID = 1 });
        peopleList1.Add(new Person() { ID = 2 });
        peopleList1.Add(new Person() { ID = 3 });

        List<Person> peopleList2 = new List<Person>();
        peopleList2.Add(new Person() { ID = 1 });
        peopleList2.Add(new Person() { ID = 2 });
        peopleList2.Add(new Person() { ID = 3 });
        peopleList2.Add(new Person() { ID = 4 });
        peopleList2.Add(new Person() { ID = 5 });
    }
}

class Person
{ …
Run Code Online (Sandbox Code Playgroud)

c# linq .net-3.5

485
推荐指数
7
解决办法
35万
查看次数

策略模式没有'切换'语句?

我一直在阅读战略模式,并有一个问题.我在下面实现了一个非常基本的控制台应用程序来解释我在问什么.

I have read that having 'switch' statements is a red flag when implementing the strategy pattern. However, I can't seem to get away from having a switch statement in this example. Am I missing something? I was able to remove the logic from the Pencil, but my Main has a switch statement in it now. I understand that I could easily create a new TriangleDrawer class, and wouldn't have to open the Pencil class, which is good. However, …

c# design-patterns dependency-injection coding-style strategy-pattern

49
推荐指数
5
解决办法
2万
查看次数

清洁代码:对象应该具有公共属性吗?

我正在阅读"清洁代码"这本书,并且正在努力解决这个问题.在讨论对象和数据结构时,它指出以下内容:

  • 对象将其数据隐藏在抽象之后,并公开对该数据进行操作的函数.
  • 数据结构公开其数据并且没有有意义的功能.

所以,我从中获得的是我不应该在我的对象上有任何公共属性,我应该只有对属性执行操作的方法.如果我确实需要访问属性,它们应该在数据结构上,可以从我对象上的方法返回?使用这种方法,似乎我需要为我的对象上的Height属性使用GetHeight()和SetHeight()方法,而不是仅使用属性的getset.

也许我不明白究竟是什么建议,但这是我对" 对象隐藏他们的数据 "的理解.如果你能帮助我理解这一点,我将非常感激!

提前致谢!

c# abstraction properties object data-structures

27
推荐指数
5
解决办法
2873
查看次数

使用LINQ,我可以验证属性是否具有所有对象的相同值?

我有一个Crate对象,它有一个KeyValuePairs列表.目前,我正在遍历每一对,以查看列表中所有项目的kvp.Value.PixelsWide是否相同.如果是,则返回true,否则返回false.

我现有的方法如下所示:

public bool Validate(Crate crate)
    {
        int firstSectionWidth = 0;
        foreach (KeyValuePair<string, SectionConfiguration> kvp in crate.Sections)
        {
            if (firstSectionWidth == 0)//first time in loop
            {
                firstSectionWidth = kvp.Value.PixelsWide;
            }
            else //not the first time in loop
            {
                if (kvp.Value.PixelsWide != firstSectionWidth)
                {
                    return false;
                }
            }
        }

        return true;
    }
Run Code Online (Sandbox Code Playgroud)

我很好奇是否可以在LINQ查询中执行?

在此先感谢您的帮助!

c# linq

27
推荐指数
3
解决办法
2万
查看次数

使用工厂模式保存数据?

我已经越来越熟悉工厂模式(以及战略模式)以及模式可以带来的巨大好处.但是,我一直在努力应对以下情况:

以前,我会做类似以下的事情,其中​​有一个经理类可以构建和保存汽车.这里没有依赖注入,并且是一个糟糕的实现,特别是在尝试单元测试时.

public class CarManager
{
    public static Car GetCarFromDatabase(int carId) { return new Car(); }

    public static void SaveCar(Car car) { }
}
Run Code Online (Sandbox Code Playgroud)

我现在看到我可以Factories为我制造不同的车,无论是来自数据库,还是来自哪里!这很棒!所以,这是我的问题:

Q1:我的理解是,Factories只应构建对象,这是正确的吗?如果是这样,我的第二个问题呢?

Q2:如果我按照工厂模式构建我的对象,我应该如何保存我的对象?这有不同的模式,还是我不完全理解工厂模式?

c# design-patterns dependency-injection strategy-pattern factory-pattern

11
推荐指数
1
解决办法
3302
查看次数

在XAML中声明的集合会挂起Silverlight

我一直在玩XAML中声明对象.我在Silverlight程序集中有这些类:

public class TextItem
{
    public string TheValue { get; set; }
}

public class TextItemCollection
{
    public ObservableCollection<TextItem> TextItems { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后,我在我的XAML中有这个:

<UserControl.Resources>
    <app:TextItemCollection x:Key="TextItemsResource">
        <app:TextItemCollection.TextItems>
            <app:TextItem TheValue="Hello world I am one of the text values"/>
            <app:TextItem TheValue="And I am another one of those text items"/>
            <app:TextItem TheValue="And I am yet a third!"/>
        </app:TextItemCollection.TextItems>
    </app:TextItemCollection>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

出于某种原因,如果我在尝试调试应用程序时包含该节点,Silverlight会挂起(我只看到旋转的蓝色加载圆圈).如果我注释掉该节点,它会立即运行.

有任何想法吗?

silverlight xaml

4
推荐指数
1
解决办法
2079
查看次数

navigator.geolocation.GetCurrentPosition抛出"最后一个位置提供程序被禁用"错误

我正在尝试创建一个非常基本的HTML5页面来抓取地理位置,但我收到了一个错误.

这是我在'script'标签中的内容:

 function GetGeo() {
        if (!navigator.geolocation) {
            alert("Could not find geolocation");
        }
        else {
            navigator.geolocation.getCurrentPosition(showMap, function (error) { alert("error encountered: " + error.message ); });
        }
    }

    function showMap(position) {
        window.alert(position.coords.latitude + ", " + position.coords.longitude);
    }
Run Code Online (Sandbox Code Playgroud)

页面上只有一个按钮,调用GetGeo()函数.

我做的是将.html文件放到我手机的SD卡上.然后,我浏览了"content://com.android.htmlfileprovider/sdcard/GetGeo.html",页面显示正确.当我单击按钮并执行时,我收到以下错误:"最后一个位置提供程序已被禁用"

我有几个问题:1:将SD文件中的html文件丢弃并尝试在那里运行是不对的?2:有没有什么基本的我做错了,不让它从移动浏览器获得地理位置?

在此先感谢您的帮助!

mobile html5 android geolocation android-2.2-froyo

4
推荐指数
1
解决办法
1万
查看次数

是否可以同步调用异步调用?

我已经在我想要做的事情下面放了一些非常基本的代码.我有'DoSomethingAshnc'方法执行异步操作.我希望'DoSomething'方法是一个不接受action参数并返回int的同步方法.

public void DoSomething(Action<int> actionToPerformOnComplete)
    {
        DoSomethingAsync(delegate(int val)
            {
                actionToPerformOnComplete(val);
            });
    }
Run Code Online (Sandbox Code Playgroud)

是否甚至可以让'DoSomething'返回一个整数,就像方法同步发生一样?

c# delegates asynchronous

3
推荐指数
1
解决办法
1409
查看次数

无法获取PropertyInfo.SetValue()来设置我的对象上的值

我已将下面的代码简化为一个基本示例,但我仍然无法获得设置的值.执行propertyInfo.SetValue()时,它将在我的Contact对象的setter上命中一个断点,并在'setter'中正确设置该值.但是,在执行SetValue()之后,projectContact.Contact对象上的字符串属性尚未更改为"a".知道我在这里做错了什么吗?

IEnumerable<ProjectContact> contacts = GetContactsByProject(projectId);

        foreach (ProjectContact projectContact in contacts)
        {
            foreach (PropertyInfo propertyInfo in projectContact.Contact.GetType().GetProperties())
            {
                if (propertyInfo.PropertyType == typeof(string))
                {
                    propertyInfo.SetValue(projectContact.Contact, "a", null);
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

c# reflection foreach properties

2
推荐指数
1
解决办法
4123
查看次数

清洁代码:可读的依赖注入建议?

我有一个项目,可以添加元素到AutoCad绘图.我注意到我开始在多种方法中编写相同的十行代码(为简单起见仅显示两行).

初步实施: 您会注意到唯一真正改变的是添加一个Line而不是Circle.

[CommandMethod("Test", CommandFlags.Session)]
    public void Test()
    {
        AddLineToDrawing();
        AddCircleToDrawing();
    }

    private void AddLineToDrawing()
    {
        using (DocumentLock lockedDocument = Application.DocumentManager.MdiActiveDocument.LockDocument())
        {
            using (Database database = Application.DocumentManager.MdiActiveDocument.Database)
            {
                using (Transaction transaction = database.TransactionManager.StartTransaction())//Start the transaction
                {
                    //Open the block table for read
                    BlockTable blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;

                    //Open the block table record model space for write
                    BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                    Line line = new Line(new Point3d(0, 0, 0), new Point3d(10, 10, 0));
                    blockTableRecord.AppendEntity(line);

                    transaction.AddNewlyCreatedDBObject(line, true);

                    transaction.Commit();
                } …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection coding-style autocad

2
推荐指数
1
解决办法
737
查看次数